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.
Hello,
I have some data I am trying to graph in this Format (see subset below) As you can see there are columns for each month of the year for each value tracked ex. Jan-Norm, Jan-Transfers. and more.
We soon need to be able to handle displaying multiple years of data and be able to select the year via a slicer. For this particular visual the project is irrelevant, we want the sum over all projects.
ProjectID | Year | Jan - Norm | Feb - Norm | Mar - Norm | Apr - Norm | May - Norm | Jun - Norm | Jul - Norm | Aug - Norm | Sep - Norm | Oct - Norm | Nov - Norm | Dec - Norm | Jan - Transfers | Feb - Transfers | Mar - Transfers | Apr - Transfers | May - Transfers | Jun - Transfers | Jul - Transfers | Aug - Transfers | Sep - Transfers | Oct - Transfers | Nov - Transfers | Dec - Transfers |
727 | 2022 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
737 | 2022 | 1 | 0 | 2 | 2 | 0 | 1 | 1 | 1 | 1 | 1 | 2 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
728 | 2022 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
730 | 2023 | 8 | 8 | 8 | 8 | 8 | 8 | 8 | 3 | 3 | 3 | 3 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
They want to see the data as a Line Chart showing the months on the bottom axis and data points as cumulative values as the year progresses broken down by each month and subfield. So with the above data set would have a line that Jan = Jan-Norm, Feb = Jan-Norm+Feb-Norm, March = Jan-Norm + Feb-Norm + Mar-Norm, etc. And a second line with the same for Jan-Transfers, Feb-Transfers
Previously this was done by using a calculated table with 12 Rows and hardcoded a month name and number to each row and added calculated columns for the cumulative. But I don't believe this will work as a slicer does not seem to work dynamically on calculated tables. I need to apply the Year Slicer filter to the above table before calculating the data below.
Thanks
Solved! Go to Solution.
Hi @tamara_nsb
You can put the following code in advanced editor in power query
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dY5BDoAgDAT/wplDaVXwLYb/f8NOIWo0HnYgZHfZ40hVa8pJRdWPUkDcDCxgBRvAWRrYIwNIeLZnr7JnlSsMUzLf3hreiGu744gJLGAA/9OOhc+LXHNtYCyQUWHT+if7qPcT", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project ID" = _t, Year = _t, Jan = _t, Feb = _t, Mar = _t, Apr = _t, May = _t, Jun = _t, Jul = _t, Aug = _t, Sep = _t, Oct = _t, Nov = _t, Dec = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Project ID", Int64.Type}, {"Year", Int64.Type}, {"Jan", Int64.Type}, {"Feb", Int64.Type}, {"Mar", Int64.Type}, {"Apr", Int64.Type}, {"May", Int64.Type}, {"Jun", Int64.Type}, {"Jul", Int64.Type}, {"Aug", Int64.Type}, {"Sep", Int64.Type}, {"Oct", Int64.Type}, {"Nov", Int64.Type}, {"Dec", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Year", "Project ID"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Month"}}),
#"Grouped Rows" = Table.Group(#"Renamed Columns", {"Month", "Year"}, {{"Sum", each List.Sum([Value]), type number}}),
#"Reordered Columns" = Table.ReorderColumns(#"Grouped Rows",{"Year", "Month", "Sum"}),
#"Added Custom" = Table.AddColumn(#"Reordered Columns", "Custom", each Date.Month(Date.FromText([Month],[Format="MMM"]))),
#"Reordered Columns1" = Table.ReorderColumns(#"Added Custom",{"Custom", "Year", "Month", "Sum"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Reordered Columns1",{{"Custom", Int64.Type}})
in
#"Changed Type1"
then apply it to report, then create a new column in table
Sum_ = SUMX(FILTER('Table (3)','Table (3)'[Custom]<=EARLIER('Table (3)'[Custom])&&'Table (3)'[Year]=EARLIER('Table (3)'[Year])),[Sum])
Best Regards,
Yolo Zhu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @tamara_nsb
You can put the following code in advanced editor in power query
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dY5BDoAgDAT/wplDaVXwLYb/f8NOIWo0HnYgZHfZ40hVa8pJRdWPUkDcDCxgBRvAWRrYIwNIeLZnr7JnlSsMUzLf3hreiGu744gJLGAA/9OOhc+LXHNtYCyQUWHT+if7qPcT", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project ID" = _t, Year = _t, Jan = _t, Feb = _t, Mar = _t, Apr = _t, May = _t, Jun = _t, Jul = _t, Aug = _t, Sep = _t, Oct = _t, Nov = _t, Dec = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Project ID", Int64.Type}, {"Year", Int64.Type}, {"Jan", Int64.Type}, {"Feb", Int64.Type}, {"Mar", Int64.Type}, {"Apr", Int64.Type}, {"May", Int64.Type}, {"Jun", Int64.Type}, {"Jul", Int64.Type}, {"Aug", Int64.Type}, {"Sep", Int64.Type}, {"Oct", Int64.Type}, {"Nov", Int64.Type}, {"Dec", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Year", "Project ID"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Month"}}),
#"Grouped Rows" = Table.Group(#"Renamed Columns", {"Month", "Year"}, {{"Sum", each List.Sum([Value]), type number}}),
#"Reordered Columns" = Table.ReorderColumns(#"Grouped Rows",{"Year", "Month", "Sum"}),
#"Added Custom" = Table.AddColumn(#"Reordered Columns", "Custom", each Date.Month(Date.FromText([Month],[Format="MMM"]))),
#"Reordered Columns1" = Table.ReorderColumns(#"Added Custom",{"Custom", "Year", "Month", "Sum"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Reordered Columns1",{{"Custom", Int64.Type}})
in
#"Changed Type1"
then apply it to report, then create a new column in table
Sum_ = SUMX(FILTER('Table (3)','Table (3)'[Custom]<=EARLIER('Table (3)'[Custom])&&'Table (3)'[Year]=EARLIER('Table (3)'[Year])),[Sum])
Best Regards,
Yolo Zhu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Thanks for this PowerQuery code. I had managed to get a working solution using the Unpivot as suggested by Mike, but your solutions is much cleaner. But in the end it was good for learning to go through trying to solve it.
Thanks for the help, sometimes you just need to know your heading in the right direction.. 🙂
Hi @tamara_nsb
Have you worked with the unpivot feature in Power Query?
Best regards
Michael
-----------------------------------------------------
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. Appreciate your thumbs up!
@ me in replies or I'll lose your thread.
-----------------------------------------------------
Thanks Mike, I had started looking at that option but was not sure it was the best solution. I wanted to make sure I am at least on the right track before trying to figure out how to unpivot the table. I will accept this as the answer and give the final solution if I am able to get it working.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
117 | |
104 | |
77 | |
73 | |
52 |
User | Count |
---|---|
145 | |
109 | |
108 | |
90 | |
64 |