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

Pivoting a table with a nasty twist

Hi all. I'm struggling with the table below, and also new to PowerBI. I've already tried using DAX, matrix reports, and so on but have got stuck on the badly placed date column. Any help would be much appreciated.

My initial table looks like this:

TaskTypeValueDate
1A24 
1B25 
1C26 
1Delivery 25/04/2021
2A31 
2B32 
2C33 
2Delivery 21/03/2021

 

I want to get the table into the following form so I can then use it for reports:

Delivery DateTaskABC
24/05/20211242526
21/03/20212313233

 

The final objective is to summarize by year:

YearSumASumBSumC
2021555759

 

As I said, any help would be very much appreciated!

1 ACCEPTED SOLUTION

A slightly different approach would be to combine the last two columns as a new custom column and then pivot Type with that new column as the values.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYiMTIKGgFKsDEXICCZmiCDmDhMxQhFxSczLLUosqwYIgDfoGJvpGBkaGYBVGUKONDeGajKBGGxuhCIGMNjZGEUI32lDfwBhqdCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Task = _t, Type = _t, Value = _t, Date = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if [Type] = "Delivery" then [Date] else [Value]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value", "Date"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Type]), "Type", "Custom"),
    #"Changed Type" = Table.TransformColumnTypes(#"Pivoted Column",{{"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}, {"Delivery", type date}})
in
    #"Changed Type"

View solution in original post

4 REPLIES 4
AlexisOlson
Super User
Super User

Use Fill Up on the Date column, remove the Delivery rows, and then pivot Value.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYiMTIKEUqwMRcQKJmCKLOINEzJBFXFJzMstSiyqBTAWwcn0DE30jAyNDsAojqLnGhjA9RlBzjY2QRUDmGhsji6Cba6hvYAw1NxYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Task = _t, Type = _t, Value = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Task", Int64.Type}, {"Type", type text}, {"Value", Int64.Type}, {"Date", type date}}),
    #"Filled Up" = Table.FillUp(#"Changed Type",{"Date"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Up", each ([Type] <> "Delivery")),
    #"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Type]), "Type", "Value")
in
    #"Pivoted Column"
Anonymous
Not applicable

Thanks for this. Trying to apply your solution on my full data, I've run into a problem, however. There isn't always a delivery date, so a simple fillup results in some tasks getting an incorrect date. Is there some way to only fill in where the task number is the same?

Also, I'm using a direct link to an SQL database. Not sure if that could potentially be an issue.

A slightly different approach would be to combine the last two columns as a new custom column and then pivot Type with that new column as the values.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYiMTIKGgFKsDEXICCZmiCDmDhMxQhFxSczLLUosqwYIgDfoGJvpGBkaGYBVGUKONDeGajKBGGxuhCIGMNjZGEUI32lDfwBhqdCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Task = _t, Type = _t, Value = _t, Date = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if [Type] = "Delivery" then [Date] else [Value]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value", "Date"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Type]), "Type", "Custom"),
    #"Changed Type" = Table.TransformColumnTypes(#"Pivoted Column",{{"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}, {"Delivery", type date}})
in
    #"Changed Type"
Anonymous
Not applicable

Great - that's done the trick, and taught me quite a bit too!

Thanks for taking time to reply.

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