Forum Discussion

WM117's avatar
WM117
Frequent Visitor
2 years ago
Solved

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

QuarterYearMonthYear
Q1-2023Jan/23
Q1-2023Feb/23
Q1-2023Mar/23
Q2-2023Apr/23
Q2-2023May/23
Q2-2023Jun/23
Q3-2023Jul/23
Q3-2023Aug/23
Q3-2023Sep/23
Q4-2023Oct/23
Q4-2023Nov/23
Q4-2023Dec/23
Q1-2024Jan/24
Q1-2024Feb/24
Q1-2024Mar/24
Q2-2024Apr/24
Q2-2024May/24
Q2-2024Jun/24
Q3-2024Jul/24
Q3-2024Aug/24
Q3-2024Sep/24
Q4-2024Oct/24
Q4-2024Nov/24
Q4-2024Dec/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_dekorte's avatar
    m_dekorte
    Resident 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

  • tackytechtom's avatar
    tackytechtom
    Most 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/

    • WM117's avatar
      WM117
      Frequent Visitor

      Thanks for helping, this worked!

  • 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