Forum Discussion

nlgenin's avatar
nlgenin
New Member
5 years ago
Solved

Creating a table with dynamic dates and one variable per date

Hello, everyone.

 

I need your kind help to create a crazy table in PowerQuery M.
What I need to do is create a table with dynamic dates range and replicate these dates for each windturbine tag.

 

So I have 151 windturbine tags and for each tag I need to have the dates from 01/01/2021 to today.

The dates will come from another table that has the min and max date;

 

In example it will be more clarified+

The table I need has to come out like this:

 

WinturbineDate
A020101/01/2021
A020101/02/2021
......
A020106/16/2021
A020201/01/2021
......
A020206/16/2021

 

So I already have a table with each windturbine tag

All I need to do is add the rows for the dates and fill down with the turbines tags.

 

 

Please help!!!

 

  • Hi nlgenin 

    do you mean you want to create the table like bellow with M code?

    Name Date
    A1 2021/1/1
    A1 2021/1/2
    A1 2021/1/3
    A1 2021/1/4
    A1 2021/1/5
    A1 .
    A1 .
    A1 .
    A1 .
    A1 2021/6/23
    A2 2021/1/1
    A2 2021/1/2
    A2 2021/1/3
    A2 2021/1/4
    A2 2021/1/5
    A2 .
    A2 .
    A2 .
    A2 .
    A2 2021/6/23

    -

    here are my operations,

    Then open the Advanced Editor, and paste the M code bellow into it

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjRUitWhnDKimIoFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}}),
        #"Removed Duplicates" = Table.Distinct(#"Changed Type"),
        diffday=Int16.From( Date.From(DateTime.LocalNow()) - Date.From(#date(2021,1,1)))+1,
        #"Added Custom" = Table.AddColumn(#"Removed Duplicates", "Custom", each List.Dates(#date(2021, 1, 1), diffday, #duration(1, 0, 0, 0))),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom")
    in
        #"Expanded Custom"

    result:

    See sample file attached bellow.

     

     

    Best Regards,

    Community Support Team _ Tang

    If this post helps, please consider Accept it as the solution✔️ to help the other members find it more quickly.

2 Replies

  • nlgenin you can add a new column in PQ like this 

    { [Min Date]..[Max Date] }

     

    it will create a list and expand the list, you will get the all the dates for each tag.

     

    Check my latest blog post Comparing Selected Client With Other Top N Clients | PeryTUS  I would ❤ Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

    Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

  • v-xiaotang's avatar
    v-xiaotang
    Community Support

    Hi nlgenin 

    do you mean you want to create the table like bellow with M code?

    Name Date
    A1 2021/1/1
    A1 2021/1/2
    A1 2021/1/3
    A1 2021/1/4
    A1 2021/1/5
    A1 .
    A1 .
    A1 .
    A1 .
    A1 2021/6/23
    A2 2021/1/1
    A2 2021/1/2
    A2 2021/1/3
    A2 2021/1/4
    A2 2021/1/5
    A2 .
    A2 .
    A2 .
    A2 .
    A2 2021/6/23

    -

    here are my operations,

    Then open the Advanced Editor, and paste the M code bellow into it

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjRUitWhnDKimIoFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}}),
        #"Removed Duplicates" = Table.Distinct(#"Changed Type"),
        diffday=Int16.From( Date.From(DateTime.LocalNow()) - Date.From(#date(2021,1,1)))+1,
        #"Added Custom" = Table.AddColumn(#"Removed Duplicates", "Custom", each List.Dates(#date(2021, 1, 1), diffday, #duration(1, 0, 0, 0))),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom")
    in
        #"Expanded Custom"

    result:

    See sample file attached bellow.

     

     

    Best Regards,

    Community Support Team _ Tang

    If this post helps, please consider Accept it as the solution✔️ to help the other members find it more quickly.