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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Ira_27
Helper I
Helper I

Power Query - Adding new row

Hello,

 

I am working with a dataset that has 15-20 columns and is represented in grouping and i need to create a new row but substracting two rows within each group. Can someone please help me on how to achive this in Power Query or DAX? 

 

Sample Data

PIDPTreeIDPtypeABCDEF
1XYZP123456
1XYZB012345
2ABCP102030405060
2ABCB-983468
3DEFP304050607080
3DEFB56789040

 

Expected Result

PIDPTreeIDPtypeABCDEF
1XYZP123456
1XYZB012345
1XYZDelta111111
2ABCP102030405060
2ABCB-983468
2ABCDelta11227364452
3DEFP304050607080
3DEFB56789040
3DEFDelta25344352-2040

 

Where Delta is difference of P and B between the same group by TreeID

2 ACCEPTED SOLUTIONS
audreygerred
Super User
Super User

Hello! Create a measure that will achieve the values whether it is a sum, count, etc.

For now, I'll just call it units: Units = SUM ('YouTable'[FieldForSumming]. Next, click the three dots next to the measure you just created and click quick measures. When the quick measure pane opens, select filtered value from the drop down. The Units measure should be in base value. For the filter part, select the field for Type and then choose P from the dropdown and click add. Next, select B from the dropdown and click add. You now have a Units for P and an Units for B measure. Next, click new measure and do Delta = [Units for P] - [Units for B].

 

In your matrix visual put your measures for P, B, and Delta in values. Go to formatting and expand Values section. Expand Options and toggle the switch so that the values will appear on Rows instead of Columns.





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

Proud to be a Super User!





View solution in original post

Anonymous
Not applicable

Hi audreygerred ,thanks for the quick reply, I'll add more.

Hi @Ira_27 ,

Regarding your question, I think DAX cannot accomplish your goal effectively, so I use Power Query.

1.Add index column after grouping (for final sorting)

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\v-zhouwenbin\Desktop\(2)2024.9.9.xlsx"), null, true),
    Table_Sheet = Source{[Item="Table",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Table_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"PID", Int64.Type}, {"PTreeID", type text}, {"Ptype", type text}, {"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}, {"D", Int64.Type}, {"E", Int64.Type}, {"F", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"PID", "PTreeID"}, {{"Group", each _, type table [PID=nullable number, PTreeID=nullable text, Ptype=nullable text, A=nullable number, B=nullable number, C=nullable number, D=nullable number, E=nullable number, F=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Group],"Index",1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"PID", "PTreeID", "Group"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"PID", "PTreeID", "Ptype", "A", "B", "C", "D", "E", "F", "Index"}, {"PID", "PTreeID", "Ptype", "A", "B", "C", "D", "E", "F", "Index"}),
    #"Appended Query" = Table.Combine({#"Expanded Custom", Table2}),
    #"Sorted Rows" = Table.Sort(#"Appended Query",{{"PID", Order.Ascending}, {"Index", Order.Ascending}})
in
    #"Sorted Rows"

 

vzhouwenmsft_1-1725864789535.png

2.Create a new query to calculate the difference between rows

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\v-zhouwenbin\Desktop\(2)2024.9.9.xlsx"), null, true),
    Table_Sheet = Source{[Item="Table",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Table_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"PID", Int64.Type}, {"PTreeID", type text}, {"Ptype", type text}, {"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}, {"D", Int64.Type}, {"E", Int64.Type}, {"F", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"PID", "PTreeID"}, {{"Group", each _, type table [PID=nullable number, PTreeID=nullable text, Ptype=nullable text, A=nullable number, B=nullable number, C=nullable number, D=nullable number, E=nullable number, F=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Group],"Index",1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"PID", "PTreeID", "Group"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"PID", "PTreeID", "Ptype", "A", "B", "C", "D", "E", "F", "Index"}, {"PID", "PTreeID", "Ptype", "A", "B", "C", "D", "E", "F", "Index"}),
    AddColumn = Table.Group(
        #"Expanded Custom",
        {"PID", "PTreeID"},
        {
            {"Ptype", each "Delta", type text},
            {"A", each List.First([A]) - Number.Abs(List.Last([A])), type number},
            {"B", each List.First([B]) - Number.Abs(List.Last([B])), type number},
            {"C", each List.First([C]) - Number.Abs(List.Last([C])), type number},
            {"D", each List.First([D]) - Number.Abs(List.Last([D])), type number},
            {"E", each List.First([E]) - Number.Abs(List.Last([E])), type number},
            {"F", each List.First([F]) - Number.Abs(List.Last([F])), type number},
            {"Index", each List.Max([Index]) + 1 , type number}
        }
    )
in
    AddColumn

 

 

vzhouwenmsft_2-1725864821367.png

3.Append two tables and sort

vzhouwenmsft_5-1725865205934.png

4.Final output

vzhouwenmsft_4-1725864924211.png

 

Best Regards,
Wenbin Zhou

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi audreygerred ,thanks for the quick reply, I'll add more.

Hi @Ira_27 ,

Regarding your question, I think DAX cannot accomplish your goal effectively, so I use Power Query.

1.Add index column after grouping (for final sorting)

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\v-zhouwenbin\Desktop\(2)2024.9.9.xlsx"), null, true),
    Table_Sheet = Source{[Item="Table",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Table_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"PID", Int64.Type}, {"PTreeID", type text}, {"Ptype", type text}, {"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}, {"D", Int64.Type}, {"E", Int64.Type}, {"F", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"PID", "PTreeID"}, {{"Group", each _, type table [PID=nullable number, PTreeID=nullable text, Ptype=nullable text, A=nullable number, B=nullable number, C=nullable number, D=nullable number, E=nullable number, F=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Group],"Index",1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"PID", "PTreeID", "Group"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"PID", "PTreeID", "Ptype", "A", "B", "C", "D", "E", "F", "Index"}, {"PID", "PTreeID", "Ptype", "A", "B", "C", "D", "E", "F", "Index"}),
    #"Appended Query" = Table.Combine({#"Expanded Custom", Table2}),
    #"Sorted Rows" = Table.Sort(#"Appended Query",{{"PID", Order.Ascending}, {"Index", Order.Ascending}})
in
    #"Sorted Rows"

 

vzhouwenmsft_1-1725864789535.png

2.Create a new query to calculate the difference between rows

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\v-zhouwenbin\Desktop\(2)2024.9.9.xlsx"), null, true),
    Table_Sheet = Source{[Item="Table",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Table_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"PID", Int64.Type}, {"PTreeID", type text}, {"Ptype", type text}, {"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}, {"D", Int64.Type}, {"E", Int64.Type}, {"F", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"PID", "PTreeID"}, {{"Group", each _, type table [PID=nullable number, PTreeID=nullable text, Ptype=nullable text, A=nullable number, B=nullable number, C=nullable number, D=nullable number, E=nullable number, F=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Group],"Index",1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"PID", "PTreeID", "Group"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"PID", "PTreeID", "Ptype", "A", "B", "C", "D", "E", "F", "Index"}, {"PID", "PTreeID", "Ptype", "A", "B", "C", "D", "E", "F", "Index"}),
    AddColumn = Table.Group(
        #"Expanded Custom",
        {"PID", "PTreeID"},
        {
            {"Ptype", each "Delta", type text},
            {"A", each List.First([A]) - Number.Abs(List.Last([A])), type number},
            {"B", each List.First([B]) - Number.Abs(List.Last([B])), type number},
            {"C", each List.First([C]) - Number.Abs(List.Last([C])), type number},
            {"D", each List.First([D]) - Number.Abs(List.Last([D])), type number},
            {"E", each List.First([E]) - Number.Abs(List.Last([E])), type number},
            {"F", each List.First([F]) - Number.Abs(List.Last([F])), type number},
            {"Index", each List.Max([Index]) + 1 , type number}
        }
    )
in
    AddColumn

 

 

vzhouwenmsft_2-1725864821367.png

3.Append two tables and sort

vzhouwenmsft_5-1725865205934.png

4.Final output

vzhouwenmsft_4-1725864924211.png

 

Best Regards,
Wenbin Zhou

audreygerred
Super User
Super User

Hello! Create a measure that will achieve the values whether it is a sum, count, etc.

For now, I'll just call it units: Units = SUM ('YouTable'[FieldForSumming]. Next, click the three dots next to the measure you just created and click quick measures. When the quick measure pane opens, select filtered value from the drop down. The Units measure should be in base value. For the filter part, select the field for Type and then choose P from the dropdown and click add. Next, select B from the dropdown and click add. You now have a Units for P and an Units for B measure. Next, click new measure and do Delta = [Units for P] - [Units for B].

 

In your matrix visual put your measures for P, B, and Delta in values. Go to formatting and expand Values section. Expand Options and toggle the switch so that the values will appear on Rows instead of Columns.





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

Proud to be a Super User!





Thank you but this wont solve the problem because i have 20 different columns that needs to be presented and needs to report delta as a seperate line. This works only if i select one column in the matrix. Any other ideas? 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.