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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Sriu2022
Regular Visitor

Subtract two rows from another based on a name column and by date category

I am trying to build a dashboard and need to subtract two specific row values from another row. For example deduct the headcount of two other leaders (say A2 and A3) from another higher level leader (say A1). I need all other rows (including the rows for leaders A2 and A3) to simply return the same value. Also, I am looking for a Power Query solution if possible.

 

Below is the Power Query formula that I am using. This works well as long as I only have one set of data with a particular date associated with it. If there is a different set of data with a different date associated with it, it does not work correctly.  Is there a way to re-write this to include the logic " For this date, Final Headcount A1 = A1-A2-A3"?

 

= Table.AddColumn(#"Trimmed Text", "Final Headcount A1", each if [Leader Name]="A1" then [Headcounts]

                          -Table.SelectRows(#"Trimmed Text",each [Leader Name]="A2"){0}[Headcounts]

                          -Table.SelectRows(#"Trimmed Text",each [Leader Name]="A3"){0}[Headcounts]

                          else [Headcounts])

 

Here are the current and result tables.

Sriu2022_0-1661550730196.jpeg

 

1 ACCEPTED SOLUTION
KT_Bsmart2gethe
Impactful Individual
Impactful Individual

Hi @Sriu2022 ,

 

Try the below modification to the provided code.

 

Table.AddColumn(#"Trimmed Text", "Final Headcount A1", each if [Leader Name]="A1" then [Headcounts]

                          -Table.SelectRows(#"Trimmed Text",(x)=> x[Leader Name]="A2" and x[Date]=[Date]){0}[Headcounts]

                          -Table.SelectRows(#"Trimmed Text",(x)=> x[Leader Name]="A3" and x[Date]=[Date]){0}[Headcounts]

                          else [Headcounts])

 

Regards

KT

View solution in original post

3 REPLIES 3
KT_Bsmart2gethe
Impactful Individual
Impactful Individual

Hi @Sriu2022 ,

 

Try the below modification to the provided code.

 

Table.AddColumn(#"Trimmed Text", "Final Headcount A1", each if [Leader Name]="A1" then [Headcounts]

                          -Table.SelectRows(#"Trimmed Text",(x)=> x[Leader Name]="A2" and x[Date]=[Date]){0}[Headcounts]

                          -Table.SelectRows(#"Trimmed Text",(x)=> x[Leader Name]="A3" and x[Date]=[Date]){0}[Headcounts]

                          else [Headcounts])

 

Regards

KT

I am getting the below error with this code. 

Expression.Error: A cyclic reference was encountered during evaluation.

 

artemus
Employee
Employee

First, you will need to create a column indicating the group name. For example the A group might be A1, A2, A3, while the B group might just be B. To do this just use Extract menu option in the transform ribbon.

 

Next, do a groupby operation with Date and the new group column you created. Use 2 aggregations:

1. The Max of HeadCount

2. The Sum of HeadCount

 

Next, subtract the max headcount from the sum headcount, removing the max headcount from the sum headcount calculation.

Finally, subtract sum headcount from the max head count.

 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Solution Authors
Top Kudoed Authors