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
Anonymous
Not applicable

Pivot column total failing if any of the pivot column missing

Hello ,

Please advice on the below issue.

Before pivot source table

File #NameEarnings codeEarnings Amount
100007nullCMM COMMISSION100
100007NullCommision 21500
100040NullCMM COMMISSION2614
100040NullCommision 20

 

After merging(left outer join)  with above table and pivoting.

File #NameReg. HoursReg. EarningsCMM COMMISSIONCommision 2Total(Cmmcomm +comm2)
100007null40100010015001600
100040null4020550261402614.69

 

New column is Total , my problem is there is a scenarion some pivot columns may not exist in future that time this total will be failed due to non availabilty of missing column. But this shouldn't be  the total is combination of multiple columns.

2 ACCEPTED SOLUTIONS
Jakinta
Solution Sage
Solution Sage

Hi,

 

2nd source table is missing, but anyway maybe this can help in transforming your 1st table.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQAAnMlHaW80pwcIOXs66vg7O/r6xkc7OnvBxQAKlCK1UFS6AdVmJ+bm1mcmZ+nYARSZUpQmSmQZwxXY2KApAbdTiMzQxPsKlEsNSCgxhzsfD1zc6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"File #" = _t, Name = _t, #"Earnings code" = _t, #"Earnings Amount" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Earnings Amount", type number}}),
    Pivoted = Table.Pivot(ChangedType, List.Distinct(ChangedType[#"Earnings code"]), "Earnings code", "Earnings Amount"),
    Cols = Table.ColumnNames(Pivoted),
    Grouped = Table.Group(Pivoted, {"File #"}, {{ "GR", each Table.FirstN( Table.FillUp(_, Cols ),1) }}),
    Removed = Table.RemoveColumns(Grouped,{"File #"}),
    Expanded = Table.ExpandTableColumn(Removed, "GR", Cols),
    Total = Table.AddColumn(Expanded, "Total", each List.Sum(List.Skip(Record.ToList(_),2)) )
in
    Total

 

Jakinta_1-1623581466919.png

View solution in original post

Anonymous
Not applicable

Hello Jakinta,

Appreciated your reply. Thankyou . Below is the second table. I will use your code and work.

singamsagar_0-1623688364700.png

 

My issue - After pivoting with left outer join with File# , in futute commisions or any other columns(after piviot) may be there or may not in that case my total will get fail due to missing columns. For this i found one more solution creating dynamic columns( creating all the required columns with out data and append this dynamic columns. when ever any missing column this will help to overwrite and avoid getting fail while doing addition

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hello Jakinta,

Appreciated your reply. Thankyou . Below is the second table. I will use your code and work.

singamsagar_0-1623688364700.png

 

My issue - After pivoting with left outer join with File# , in futute commisions or any other columns(after piviot) may be there or may not in that case my total will get fail due to missing columns. For this i found one more solution creating dynamic columns( creating all the required columns with out data and append this dynamic columns. when ever any missing column this will help to overwrite and avoid getting fail while doing addition

 

Jakinta
Solution Sage
Solution Sage

Hi,

 

2nd source table is missing, but anyway maybe this can help in transforming your 1st table.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQAAnMlHaW80pwcIOXs66vg7O/r6xkc7OnvBxQAKlCK1UFS6AdVmJ+bm1mcmZ+nYARSZUpQmSmQZwxXY2KApAbdTiMzQxPsKlEsNSCgxhzsfD1zc6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"File #" = _t, Name = _t, #"Earnings code" = _t, #"Earnings Amount" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Earnings Amount", type number}}),
    Pivoted = Table.Pivot(ChangedType, List.Distinct(ChangedType[#"Earnings code"]), "Earnings code", "Earnings Amount"),
    Cols = Table.ColumnNames(Pivoted),
    Grouped = Table.Group(Pivoted, {"File #"}, {{ "GR", each Table.FirstN( Table.FillUp(_, Cols ),1) }}),
    Removed = Table.RemoveColumns(Grouped,{"File #"}),
    Expanded = Table.ExpandTableColumn(Removed, "GR", Cols),
    Total = Table.AddColumn(Expanded, "Total", each List.Sum(List.Skip(Record.ToList(_),2)) )
in
    Total

 

Jakinta_1-1623581466919.png

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