Apps

Apps

Mega Table SubTotals in Brick

I am lost in how subtotals are integrated into a mega table in a brick. I started with the ten data source DDX brick, and I was comfortable with configuring the options and the two data queries; however, the subtotal has me stumped. Is the subtotal a third query or somehow handled with the data from the original query? I have attempted it both ways. Currently, subtotal rows do not appear for each Broker_Co. and the button to collapse is visible but does not do anything. Any pointers would be appreciated.

Tagged:

Best Answer

  • Coach
    Answer ✓

    This is a bit out of my area of reach, but I'll give it a go.

    Based on what I read and see in your image, it looks like you are working with a brick that involves making an API call to a Domo dataset. Then performing calculations, and possibly aggregating data with subtotals.

    To calculate subtotals and a grand total using API calls within a Domo brick, I'd try something like this:

    1. // Define dataset IDs and fields
    2. const datasetId = datasets[config.datasetIndex];
    3. const fields = "Broker_Co., Sales";


    4. // Fetch data from the dataset
    5. const fetchData = async (query) => {
    6. const response = await fetch(query);
    7. return await response.json();
    8. };


    9. // Query to get subtotals
    10. const subtotalQuery = `data/v1/${datasetId}?fields=${fields}&groupby=Broker_Co.&aggregate=sum(Sales)`;


    11. // Query to get grand total
    12. const totalQuery = `data/v1/${datasetId}?fields=${fields}&aggregate=sum(Sales)`;


    13. // Fetch the subtotal data
    14. const fetchSubtotalData = async () => {
    15. const data = await fetchData(subtotalQuery);
    16. return data;
    17. };


    18. // Fetch the grand total data
    19. const fetchTotalData = async () => {
    20. const data = await fetchData(totalQuery);
    21. return data;
    22. };


    23. // Main function to get and process the data
    24. const processData = async () => {
    25. try {
    26. const subtotalData = await fetchSubtotalData();
    27. const totalData = await fetchTotalData();

    28. // Process subtotal data
    29. const subtotalRows = subtotalData.map(row => ({
    30. Broker_Co: row.Broker_Co,
    31. Total_Sales: row.Sales_sum
    32. }));

    33. // Add grand total row
    34. const grandTotal = totalData[0]?.Sales_sum || 0;
    35. subtotalRows.push({
    36. Broker_Co: 'Grand Total',
    37. Total_Sales: grandTotal
    38. });

    39. return subtotalRows;
    40. } catch (error) {
    41. console.error('Error fetching data:', error);
    42. return [];
    43. }
    44. };


    45. // Execute the data processing
    46. processData().then(result => {
    47. console.log(result);
    48. // You would typically return the result here to Domo
    49. });

    ** Was this post helpful? Click Agree or Like below. **
    ** Did this solve your problem? Accept it as a solution! **

Answers

  • Coach
    Answer ✓

    This is a bit out of my area of reach, but I'll give it a go.

    Based on what I read and see in your image, it looks like you are working with a brick that involves making an API call to a Domo dataset. Then performing calculations, and possibly aggregating data with subtotals.

    To calculate subtotals and a grand total using API calls within a Domo brick, I'd try something like this:

    1. // Define dataset IDs and fields
    2. const datasetId = datasets[config.datasetIndex];
    3. const fields = "Broker_Co., Sales";


    4. // Fetch data from the dataset
    5. const fetchData = async (query) => {
    6. const response = await fetch(query);
    7. return await response.json();
    8. };


    9. // Query to get subtotals
    10. const subtotalQuery = `data/v1/${datasetId}?fields=${fields}&groupby=Broker_Co.&aggregate=sum(Sales)`;


    11. // Query to get grand total
    12. const totalQuery = `data/v1/${datasetId}?fields=${fields}&aggregate=sum(Sales)`;


    13. // Fetch the subtotal data
    14. const fetchSubtotalData = async () => {
    15. const data = await fetchData(subtotalQuery);
    16. return data;
    17. };


    18. // Fetch the grand total data
    19. const fetchTotalData = async () => {
    20. const data = await fetchData(totalQuery);
    21. return data;
    22. };


    23. // Main function to get and process the data
    24. const processData = async () => {
    25. try {
    26. const subtotalData = await fetchSubtotalData();
    27. const totalData = await fetchTotalData();

    28. // Process subtotal data
    29. const subtotalRows = subtotalData.map(row => ({
    30. Broker_Co: row.Broker_Co,
    31. Total_Sales: row.Sales_sum
    32. }));

    33. // Add grand total row
    34. const grandTotal = totalData[0]?.Sales_sum || 0;
    35. subtotalRows.push({
    36. Broker_Co: 'Grand Total',
    37. Total_Sales: grandTotal
    38. });

    39. return subtotalRows;
    40. } catch (error) {
    41. console.error('Error fetching data:', error);
    42. return [];
    43. }
    44. };


    45. // Execute the data processing
    46. processData().then(result => {
    47. console.log(result);
    48. // You would typically return the result here to Domo
    49. });

    ** Was this post helpful? Click Agree or Like below. **
    ** Did this solve your problem? Accept it as a solution! **

Welcome!

It looks like you're new here. Members get access to exclusive content, events, rewards, and more. Sign in or register to get started.
Sign In