Forum Discussion
WM117
2 years agoFrequent Visitor
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 ...
- 2 years ago
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/
m_dekorte
2 years agoResident 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
ExpandDates
That will return a list with values you can expand, final result below.
I hope this is helpful