cancel
Showing results for
Did you mean:
Frequent Visitor

## Convert Monthly Columns To Rows and plotting

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.

Totals = {
(1, "Jan", SUM(input[Jan - Norm]), SUM(input[Jan - Transfers]),
(2, "Feb", SUM(input[Feb - Norm]), SUM(input[Feb- Transfers]),
...
(2, "Feb", SUM(input[Dec - Norm]), SUM(input[Dec- Transfers]),

I beleive I need to transpose or unpivot the table to get the data in the format I need but can't figure out how.   Maybe Sum or Group by Year first then try to get the Jan - Field columns into rows and split around Month name?  I do have a Date table with months in it.
Also I have been trying to create measures with the original table that will do this also.  These measures change with the slicer as I want, I can display them in a table but now how do I relate these measures into a chart with Months?
Jan - CumNormTotal = CALCULATE(SUM('input'[Jan - Norm]))
Feb - CumNormTotal = CALCULATE(SUM('input'[Jan - Norm]) + SUM('input'[Feb - Norm])))

Any help would be appreciated.  I am open to any ideas on how to better handle this data to display what is required,  it seems like it should be pretty easy?  Is there another way besides a slicer that a user can filter this table by selecting a year and have it affect the calculated table?  I am new to Power BI and maybe I am just not aware of some of the basics.

Thanks

1 ACCEPTED SOLUTION
Community Support

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"}),

#"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.

4 REPLIES 4
Community Support

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"}),

#"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.

Frequent Visitor

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.. 🙂

Resident Rockstar

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!

-----------------------------------------------------

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!
Frequent Visitor

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.

Announcements

#### 2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

#### Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

#### Business Application LATAM Summit 2023

Join the biggest FREE Business Applications Event in LATAM this February.

#### Global Power Platform Bootcamp

In this bootcamp we will deep-dive into Microsoft’s Power Platform stack with hands-on sessions and labs, delivered to you by experts and community leaders.

Top Solution Authors
Top Kudoed Authors