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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
peterso
Helper II
Helper II

Adding custom rows in Power Query

Hi everyone,

 

I've got a bit of an interesting problem here that I can't seem to solve. Wondering if you may know the answer to this.

 

Based on each distinct "Assignment", I need to add two rows:

 

Row 1

"Assignment" row should reflect the current Assignment number

"Scopes" row should reflect Assignment.CustomText

"Labor" should reflect $0

"Materials" should reflect $0

"Rent" should be the SUM of rents for that particular assignment

 

Row 2

"Assignment" row should reflect the current Assignment number

"Scopes" row should reflect Assignment.AnotherCustomText

"Labor" should reflect $0

"Materials" should reflect $0

"Rent" should reflect $0

 

Thanks in advance!

 

DATA

Data.PNG

 

 

 

 

 

 

 

 

 

EXPECTED RESULT

Result.PNG

@harshnathani  - do you have any ideas on this?

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @peterso 

 

you can apply Table.Group twice to your orignal data and then combine your source with the results of your Table.Groups

Here the code to understand it better

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUrMq1QoSa0oATJNodjCQClWB0MWxDUD0UYQaSNUaWMgNgfTWCTBpoJooNZYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Assignment = _t, Scopes = _t, Labor = _t, Materials = _t, Rent = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Assignment", Int64.Type}, {"Scopes", type text}, {"Labor", Int64.Type}, {"Materials", Int64.Type}, {"Rent", Int64.Type}}),
    CreateCustomTextRow = Table.Group(#"Changed Type", {"Assignment"}, {{"Rent", each List.Sum([Rent]), type number}, {"Scopes", each Text.From([Assignment]{0})&".CustomText"}}),
    CreateAnotherCustomTextRow = Table.Group(#"Changed Type", {"Assignment"}, {{"Rent", each 0, type number}, {"Scopes", each Text.From([Assignment]{0})&".AnotherCustomText"}}),
    Combine = Table.Combine({#"Changed Type", CreateCustomTextRow, CreateAnotherCustomTextRow})
in
    Combine

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

8 REPLIES 8
Jimmy801
Community Champion
Community Champion

Hello @peterso 

 

you can apply Table.Group twice to your orignal data and then combine your source with the results of your Table.Groups

Here the code to understand it better

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUrMq1QoSa0oATJNodjCQClWB0MWxDUD0UYQaSNUaWMgNgfTWCTBpoJooNZYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Assignment = _t, Scopes = _t, Labor = _t, Materials = _t, Rent = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Assignment", Int64.Type}, {"Scopes", type text}, {"Labor", Int64.Type}, {"Materials", Int64.Type}, {"Rent", Int64.Type}}),
    CreateCustomTextRow = Table.Group(#"Changed Type", {"Assignment"}, {{"Rent", each List.Sum([Rent]), type number}, {"Scopes", each Text.From([Assignment]{0})&".CustomText"}}),
    CreateAnotherCustomTextRow = Table.Group(#"Changed Type", {"Assignment"}, {{"Rent", each 0, type number}, {"Scopes", each Text.From([Assignment]{0})&".AnotherCustomText"}}),
    Combine = Table.Combine({#"Changed Type", CreateCustomTextRow, CreateAnotherCustomTextRow})
in
    Combine

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Hey Jimmy - I got a cyclical reference. Wondering why that is.

Jimmy801
Community Champion
Community Champion

Hello @peterso 

 

then probably is there a cyclical error 🙂

When you post your code, I can check it out

 

BR

 

Jimmy

v-alq-msft
Community Support
Community Support

Hi, @peterso 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Data:

c1.png

 

You may create a blank query and paste the following codes in the corresponding 'Advanced Editor'.

let
    Source1 = Data[Assignment],
    #"Converted to Table" = Table.FromList(Source1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Custom1" = Table.Distinct(#"Converted to Table"),
    #"Renamed Columns" = Table.RenameColumns(Custom1,{{"Column1", "Assignment"}}),
    #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Scopes", each Text.From([Assignment])&".customtext"),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Labor", each 0),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Materials", each 0),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Rent", each let 
assignment=[Assignment], 
tab = Table.SelectRows(Data,each [Assignment]=assignment),
sum = List.Sum(tab[Rent])
in 
sum),
    Source2 = Data[Assignment],
    #"Converted to Table2" = Table.FromList(Source2, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Custom2" = Table.Distinct(#"Converted to Table2"),
    #"Renamed Columns2" = Table.RenameColumns(Custom2,{{"Column1", "Assignment"}}),
    #"Added Custom4" = Table.AddColumn(#"Renamed Columns2", "Scopes", each Text.From([Assignment])&".anothercustomtext"),
    #"Added Custom5" = Table.AddColumn(#"Added Custom4", "Labor", each 0),
    #"Added Custom6" = Table.AddColumn(#"Added Custom5", "Materials", each 0),
    #"Added Custom7" = Table.AddColumn(#"Added Custom6", "Rent", each 0),
    Source = Table.Combine({#"Added Custom3",#"Added Custom7"}),
    #"Sorted Rows" = Table.Sort(Source,{{"Assignment", Order.Ascending}}),
    #"Result" = Table.Combine({Data,#"Sorted Rows"})

in
    #"Result"

 

c2.png

 

Result:

c3.png

 

Best Regards

Allan

 

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

CNENFRNL
Community Champion
Community Champion

Hi, there

Just unpivot such a table by columns "customtext" and "anothercustomertext", then append to the table "DATA".

Untitled.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

I don't quite understand. Can you explain please?

CNENFRNL
Community Champion
Community Champion

Pls refer to the attached file  for the procedure.


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

There is a circular reference error. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.

Top Solution Authors
Top Kudoed Authors