Forum Discussion
Generate new rows based on Quarter Year table in Power Query
Hi Everybody,
I want to generate extra rows in my dataset based on the column QuarterYear
Current situation
| QuarterYear |
| Q1-2023 |
| Q2-2023 |
| Q3-2023 |
| Q4-2023 |
| Q1-2024 |
| Q2-2024 |
| Q3-2024 |
| Q4-2024 |
| Q1-2025 |
| Q2-2025 |
| Q3-2025 |
| Q4-2025 |
Desired situation
| QuarterYear | MonthYear |
| Q1-2023 | Jan/23 |
| Q1-2023 | Feb/23 |
| Q1-2023 | Mar/23 |
| Q2-2023 | Apr/23 |
| Q2-2023 | May/23 |
| Q2-2023 | Jun/23 |
| Q3-2023 | Jul/23 |
| Q3-2023 | Aug/23 |
| Q3-2023 | Sep/23 |
| Q4-2023 | Oct/23 |
| Q4-2023 | Nov/23 |
| Q4-2023 | Dec/23 |
| Q1-2024 | Jan/24 |
| Q1-2024 | Feb/24 |
| Q1-2024 | Mar/24 |
| Q2-2024 | Apr/24 |
| Q2-2024 | May/24 |
| Q2-2024 | Jun/24 |
| Q3-2024 | Jul/24 |
| Q3-2024 | Aug/24 |
| Q3-2024 | Sep/24 |
| Q4-2024 | Oct/24 |
| Q4-2024 | Nov/24 |
| Q4-2024 | Dec/24 |
Does somebody have the solution in PowerQuery?
Thanks!
Hi WM117 ,
How about this:
I first created a lookup table called TableQM specifying the months per quarter:
Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TcqxDQAgCEXBXaw1UfgMYk3Yfw2J1euuuMxxz7JtPmq2DXZY8P/CF77whR/4gR/47Xo=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [QuarterYear = _t]), #"Added Custom" = Table.AddColumn(Source, "Custom", each [QuarterYear]), #"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "Custom", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"Custom.1", "Custom.2"}), #"Merged Queries" = Table.NestedJoin(#"Split Column by Delimiter", {"Custom.1"}, TableQM, {"Quarter"}, "TableQM", JoinKind.LeftOuter), #"Expanded TableQM" = Table.ExpandTableColumn(#"Merged Queries", "TableQM", {"Month"}, {"TableQM.Month"}), #"Added Custom1" = Table.AddColumn(#"Expanded TableQM", "Custom", each [TableQM.Month] & "/" & Text.End([Custom.2], 2)), #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom.1", "Custom.2", "TableQM.Month"}) in #"Removed Columns"Let me know if this solves the issue 🙂
/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/
4 Replies
- m_dekorteResident Rockstar
Hi WM117,
Sound and low code solution by tackytechtom
However if you are set on "calculating" those dates, you can try something like this:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TcqxDQAgCEXBXaw1UfgMYk3Yfw2J1euuuMxxz7JtPmq2DXZY8P/CF77whR/4gR/47Xo=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [QuarterYear = _t]), AddDates = Table.AddColumn(Source, "Custom", each [ Q = Number.From( Text.ToList([QuarterYear]){1} ), Y = Text.End( [QuarterYear], 4), M = List.Transform( List.Range( {1..12}, if Q = 1 then 0 else (Q-1) *3, 3 ), (x)=> Date.ToText( Date.FromText( Text.From(x) & "-" & Y ), [Format="MMM/yy", Culture="en-US"] )) ][M] ), ExpandDates = Table.ExpandListColumn(AddDates, "Custom") in ExpandDatesThat will return a list with values you can expand, final result below.
I hope this is helpful
- tackytechtomMost Valuable Professional
Hi WM117 ,
How about this:
I first created a lookup table called TableQM specifying the months per quarter:
Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TcqxDQAgCEXBXaw1UfgMYk3Yfw2J1euuuMxxz7JtPmq2DXZY8P/CF77whR/4gR/47Xo=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [QuarterYear = _t]), #"Added Custom" = Table.AddColumn(Source, "Custom", each [QuarterYear]), #"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "Custom", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"Custom.1", "Custom.2"}), #"Merged Queries" = Table.NestedJoin(#"Split Column by Delimiter", {"Custom.1"}, TableQM, {"Quarter"}, "TableQM", JoinKind.LeftOuter), #"Expanded TableQM" = Table.ExpandTableColumn(#"Merged Queries", "TableQM", {"Month"}, {"TableQM.Month"}), #"Added Custom1" = Table.AddColumn(#"Expanded TableQM", "Custom", each [TableQM.Month] & "/" & Text.End([Custom.2], 2)), #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom.1", "Custom.2", "TableQM.Month"}) in #"Removed Columns"Let me know if this solves the issue 🙂
/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/- WM117Frequent Visitor
Thanks for helping, this worked!
- AlienSxSuper User
Hi, WM117 another text field solution
let Source = situation, quarters = [Q1 = {"Jan/", "Feb/", "Mar/"}, Q2 = {"Apr/", "May/", "Jun/"}, Q3 = {"Jul/", "Aug/", "Sep/"}, Q4 = {"Oct/", "Nov/", "Dec/"}], add = Table.AddColumn( Source, "MonthYear", (x) => List.Transform( Record.FieldOrDefault(quarters, Text.Start(x[QuarterYear], 2)), (w) => w & Text.End(x[QuarterYear], 2) ) ), expand = Table.ExpandListColumn(add, "MonthYear") in expand