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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

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
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors