Forum Discussion

rupeshghosh18's avatar
rupeshghosh18
Regular Visitor
4 years ago
Solved

Running Total with categories

Hi all, 

I want to calculate running_total for the last seven days for a metric but also involve the category.

I have created a formula as below,

Running Submissions = CALCULATE(SUM('Main Query'[Submissions]),FILTER(ALLSELECTED('Main Query'[date]),'Main Query'[date] <= MAX('Main Query'[date])))
 
This works well when I do not involve any category into it as seen below, this graph has a date relative filter showing the 'Last 7 days'

 

But as soon I involve a category, in the legend to see the running total by each category, that messes up the graph as you can see below.

 

My thought is since not all categories are available on each date the bars are not increasing as it can be seen in the first graph (shown in blue)

 

Is there a fix for this?

 

Thank you.

 

  • Hi rupeshghosh18 ,

    According to your description, not all categories are available on each date, this is what is causing the problem.

    Here's my sample, there is no category C in date 2/7/2022.

    It display as yours. In the date 2/7/2022, there is no category C, so the Running Submissions can't calculate by category C.

    Here's my solution.

    1.In Power Query, right click Main Query table, then select Duplicate.

    2.Select date column, then click Remove Columns>Remove Other Columns.

    3.Select Remove Rows>Remove Duplicates.

    4.Select Custom Column.

    Write the below formula, click OK.

    =#"Main Query"[Category]

    5.Click the expand tab in the right upper coner of the custom column, then select Expand to New Rows.

    6.Select the two columns at the same time(Ctrl+select), then click Remove Rows>Remove Duplicates.

    7.Rename the custom column name, get this table.

    8.Create a calculated column in new table.

    Submissions = 
    VAR _SUB =
        MAXX (
            FILTER (
                ALL ( 'Main Query' ),
                'Main Query'[date] = EARLIER ( 'Main Query (2)'[date] )
                    && 'Main Query'[Category] = EARLIER ( 'Main Query (2)'[Category] )
            ),
            'Main Query'[Submissions]
        )
    RETURN
        IF ( _SUB <> BLANK (), _SUB, 0 )
    

    9.Create a measure

    Running Submissions2 = 
    CALCULATE (
        SUM ( 'Main Query (2)'[Submissions] ),
        FILTER (
            ALLSELECTED ( 'Main Query (2)'[date] ),
            'Main Query (2)'[date] <= MAX ( 'Main Query (2)'[date] )
        )
    )
    

    10.Get the expected result.

    I attach my sample below for reference.

     

    Best Regards,
    Community Support Team _ kalyj

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

     

5 Replies

  • Hi rupeshghosh18 ,

    According to your description, not all categories are available on each date, this is what is causing the problem.

    Here's my sample, there is no category C in date 2/7/2022.

    It display as yours. In the date 2/7/2022, there is no category C, so the Running Submissions can't calculate by category C.

    Here's my solution.

    1.In Power Query, right click Main Query table, then select Duplicate.

    2.Select date column, then click Remove Columns>Remove Other Columns.

    3.Select Remove Rows>Remove Duplicates.

    4.Select Custom Column.

    Write the below formula, click OK.

    =#"Main Query"[Category]

    5.Click the expand tab in the right upper coner of the custom column, then select Expand to New Rows.

    6.Select the two columns at the same time(Ctrl+select), then click Remove Rows>Remove Duplicates.

    7.Rename the custom column name, get this table.

    8.Create a calculated column in new table.

    Submissions = 
    VAR _SUB =
        MAXX (
            FILTER (
                ALL ( 'Main Query' ),
                'Main Query'[date] = EARLIER ( 'Main Query (2)'[date] )
                    && 'Main Query'[Category] = EARLIER ( 'Main Query (2)'[Category] )
            ),
            'Main Query'[Submissions]
        )
    RETURN
        IF ( _SUB <> BLANK (), _SUB, 0 )
    

    9.Create a measure

    Running Submissions2 = 
    CALCULATE (
        SUM ( 'Main Query (2)'[Submissions] ),
        FILTER (
            ALLSELECTED ( 'Main Query (2)'[date] ),
            'Main Query (2)'[date] <= MAX ( 'Main Query (2)'[date] )
        )
    )
    

    10.Get the expected result.

    I attach my sample below for reference.

     

    Best Regards,
    Community Support Team _ kalyj

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

     

  • Hello,

    You should put another filter condition with or: 



    CALCULATE(SUM('Main Query'[Submissions]),FILTER(ALLSELECTED('Main Query'[date]),'Main Query'[date] <= MAX('Main Query'[date] 

    || ( category...... )
    )))


    Do category and date are in the same table?

    If you send a file, it would easier for me to help you

    • rupeshghosh18's avatar
      rupeshghosh18
      Regular Visitor

      Unfortunately, I don't see an option to attach a file in the comments and also no option to edit my question and attach a file

    • rupeshghosh18's avatar
      rupeshghosh18
      Regular Visitor

      Hello, thanks for responding.

      Yes both are in the same table.