Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Jerome453
Frequent Visitor

Running totals with multiple related tables

I am trying to calculate running totals for sales from a few linked by IDs tables by using the following:

 

Amount Measure = SUMX( Sales, Sales[Amount] )
Running Total = 
VAR MinAmount = 
    MINX( ALLSELECTED(Sales[Client ID]), [Amount Measure] )
VAR RT = 
    CALCULATE(
        SUMX(
            FILTER(ALLSELECTED(Sales[Client ID]), [Amount Measure] >= MinAmount),
            [Amount Measure]
        ),
        CALCULATETABLE(ALL(Clients))
    )
RETURN
    RT

 

but the resulting table shows all of the clients for the selected employee.

sample_ss.png

What can be done to show only the selected employee clients?

 

Sample data

Employees table:

Employee IDEmployee NameTarget Amount
1Donna Harper12000
2Tyrese Coffey11000
3Gisselle Bonilla15000
4Patrick Leach10000

Clients table:

Client IDClient Name
1Harris - Beatty
2Kohler, Terry and Waters
3Walsh - Hilpert
4Waelchi LLC
5Spinka LLC
6Mills, Wyman and Yundt
7Nienow - Miller
8Armstrong - Steuber
9Schultz - Bogisich

Sales table:

DateSalesperson IDClient IDAmount
01.01.202313150
01.01.2023245000
01.01.202335600
01.01.20234375
01.01.202314200
01.01.202325954
01.01.202333147
01.01.2023442223
01.01.202315478
02.01.202323200
02.01.202334653
02.01.202345258
02.01.202313552
02.01.202324478
02.01.202335269
02.01.2023463888

 

 

1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hi @Jerome453 ,

 

Let me start by refering that the Amount measure does not need to be a SUMX you can simply do a SUM, SUMX is used when you want to have a calculation maintaining the row context of the table in this case there is no need, regarding the Running total you can try and replace your code by the one below:

Amount Measure = SUM( Sales[Amount] )

 

Running Total = 
VAR MinAmount = 
    MINX( ALLSELECTED(Sales[Client ID]), [Amount Measure] )
VAR RT = 
    CALCULATE(
        SUMX(
            FILTER(ALLSELECTED(Sales[Client ID]), [Amount Measure] >= MinAmount),
            [Amount Measure]
        ),
        CALCULATETABLE(ALL(Clients))
    )
RETURN
    RT

 

MFelix_0-1676659347951.png

 

On a best practice note you should use the dimension tables and only the columns you need and not the sales table redo the measure to:

Running Total = 
VAR MinAmount = 
    MINX( ALLSELECTED(Clients[Client ID]), [Amount Measure] )
VAR RT = 
    CALCULATE(
        SUMX(
            FILTER(ALLSELECTED(Clients[Client ID]), [Amount Measure] >= MinAmount),
            [Amount Measure]
        ),
        CALCULATETABLE(ALL(Clients[Client ID], Clients[Client Name]))
    )
RETURN
  IF([Amount Measure] <> BLANK(),   RT)

Same result:

 

MFelix_2-1676659571719.png

But be aware that this logic is only working when you select a single employee if you select more than one the the runnig total is incorrect.

 

If you want the running total to work with all the values redo the measure to:

Running Total = 
VAR MinAmount = 
    MINX( ALLSELECTED(Clients[Client ID]), [Amount Measure] )
VAR RT = 
    CALCULATE(
        SUMX(
            FILTER(SUMMARIZE(Sales, Clients[Client ID], Employees[Employee ID]), [Amount Measure] >= MinAmount),
            [Amount Measure]
        ),
        ALLSELECTED(Clients[Client ID], Clients[Client Name]), ALLSELECTED(Employees)
    )
RETURN
  IF([Amount Measure] <> BLANK(),   RT)

 

MFelix_4-1676659843746.png

 

Check PBIX file attach.

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

6 REPLIES 6
MFelix
Super User
Super User

Hi @Jerome453 ,

 

Let me start by refering that the Amount measure does not need to be a SUMX you can simply do a SUM, SUMX is used when you want to have a calculation maintaining the row context of the table in this case there is no need, regarding the Running total you can try and replace your code by the one below:

Amount Measure = SUM( Sales[Amount] )

 

Running Total = 
VAR MinAmount = 
    MINX( ALLSELECTED(Sales[Client ID]), [Amount Measure] )
VAR RT = 
    CALCULATE(
        SUMX(
            FILTER(ALLSELECTED(Sales[Client ID]), [Amount Measure] >= MinAmount),
            [Amount Measure]
        ),
        CALCULATETABLE(ALL(Clients))
    )
RETURN
    RT

 

MFelix_0-1676659347951.png

 

On a best practice note you should use the dimension tables and only the columns you need and not the sales table redo the measure to:

Running Total = 
VAR MinAmount = 
    MINX( ALLSELECTED(Clients[Client ID]), [Amount Measure] )
VAR RT = 
    CALCULATE(
        SUMX(
            FILTER(ALLSELECTED(Clients[Client ID]), [Amount Measure] >= MinAmount),
            [Amount Measure]
        ),
        CALCULATETABLE(ALL(Clients[Client ID], Clients[Client Name]))
    )
RETURN
  IF([Amount Measure] <> BLANK(),   RT)

Same result:

 

MFelix_2-1676659571719.png

But be aware that this logic is only working when you select a single employee if you select more than one the the runnig total is incorrect.

 

If you want the running total to work with all the values redo the measure to:

Running Total = 
VAR MinAmount = 
    MINX( ALLSELECTED(Clients[Client ID]), [Amount Measure] )
VAR RT = 
    CALCULATE(
        SUMX(
            FILTER(SUMMARIZE(Sales, Clients[Client ID], Employees[Employee ID]), [Amount Measure] >= MinAmount),
            [Amount Measure]
        ),
        ALLSELECTED(Clients[Client ID], Clients[Client Name]), ALLSELECTED(Employees)
    )
RETURN
  IF([Amount Measure] <> BLANK(),   RT)

 

MFelix_4-1676659843746.png

 

Check PBIX file attach.

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hello @MFelix,
Thank you for the response. Your solution works. Still, i noticed that when more than one employee is selected the grand total for running total is incorrect using your latest modification.

How would one go about fixing that?

Hi @Jerome453,

 

Sorry for the late response try the following code:

 

Running Total =
VAR MinAmount =
    MINX ( ALLSELECTED ( Clients[Client ID] ), [Amount Measure] )
VAR RT =
    CALCULATE (
        SUMX (
            FILTER (
                SUMMARIZE ( Sales, Clients[Client ID], Employees[Employee ID] ),
                [Amount Measure] >= MinAmount
            ),
            [Amount Measure]
        ),
        ALLSELECTED ( Clients[Client ID], Clients[Client Name] ),
        ALLSELECTED ( Employees )
    )
RETURN
    IF (
        ISINSCOPE ( Clients[Client Name] ) || ISINSCOPE ( Clients[Employee Name] ),
        IF ( [Amount Measure] <> BLANK (), RT ),
        [Amount Measure]
    )

Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Thank you @MFelix ,
I am not familiar with ISINSCOPE(), but i did find a solution by using HASONEVALUE().

On a related issue, im trying to count the clients under a certain % RT out of total amount by using

% RT From Total Amount = 
VAR TotalAmount = 
    CALCULATE(
        SUMX(
            SUMMARIZE(Sales, Clients[Client ID], Employees[Employee ID]),
            [Amount Measure]
        ),
        ALLSELECTED(Clients[Client ID], Clients[Client Name]), ALLSELECTED(Employees)
    )
RETURN
    DIVIDE([Running Total], TotalAmount)
Count Top 95% = 
VAR TopClients =
    CALCULATETABLE (
        FILTER (
            SUMMARIZE (
                Sales,
                Employees[Employee ID],
                Clients[Client ID],
                Clients[Client Name]
            ),
            [% RT From Total Amount] <= 0.95
        ),
        ALLSELECTED ( Clients[Client ID], Clients[Client Name] )
    )
RETURN
    IF ( [Amount Measure] <> BLANK (), COUNTROWS ( TopClients ) )

but the total is not correct and i dont know how to calculate it.

sample_ss.png

Any advice would be great.
Link to sample pbix file

https://drive.google.com/file/d/1M22AgOaKl1_q3656hp92N_pBnQdHQ2Xq/view?usp=sharing

Hi @Jerome453 ,

 

If you want only the values for the employee name try the following measure:

 

Total Rows= SUMX(VALUES(Employees[Employee ID]), [Count Top 95%])

 

MFelix_1-1677489580693.pngMFelix_2-1677489584601.png

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Thanks @MFelix , this is exactly what i needed!

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.