Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Duplicate rows and create date columns

Hi the community !

 

I need your help for a trick.

To explain it quickly and simply, below is what I have and what I want in the query editor :

 

Starting point :

LOCATION
PLANT A
PLANT B

 

Expected result :

LOCATIONSTART_DATEEND_DATE
PLANT A2021-01-012021-01-31
PLANT A2021-02-012021-02-28
.........
PLANT A2021-12-012021-12-31
PLANT B2021-01-012021-01-31
.........
PLANT B2021-12-012021-12-31

 

A first solution I imagined is to create one column for every date and then unpivot it but it's not efficient at all.

Does someone knows a nice trick to do it easily and with more class ?

 

Best regards 

Cado

1 ACCEPTED SOLUTION

@Anonymous ,

 

No problem.

 

In Power Query, create a new blank query then paste this over the default code in Advanced Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvBx9AtRcFSK1YGxnZRiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [LOCATION = _t]),
    addSTART_DATE = Table.AddColumn(Source, "START_DATE", each List.Distinct(List.Transform({Number.From(Date.StartOfYear(DateTime.LocalNow()))..Number.From(Date.From(DateTime.LocalNow()))}, each Date.StartOfMonth(Date.From(_))))),
    expandSTART_DATE = Table.ExpandListColumn(addSTART_DATE, "START_DATE"),
    addEND_DATE = Table.AddColumn(expandSTART_DATE, "END_DATE", each Date.EndOfMonth([START_DATE])),
    chgTypes = Table.TransformColumnTypes(addEND_DATE,{{"START_DATE", type date}, {"END_DATE", type date}})
in
    chgTypes

 

You can now follow the steps I took to complete this.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

4 REPLIES 4
BA_Pete
Super User
Super User

Hi @Anonymous ,

 

This is relatively easy to do I think, but I need to know where your dates/date ranges are coming from?

 

*EDIT* It looks like it's just Start of January | End of January, Start of Februaruy | End of February for each PLANT. Is that correct?

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Anonymous
Not applicable

Hi @BA_Pete 

 

The START_DATE column corresponds to the first day of every month in the year, the END_DATE column corresponds to the last day of every month in the year.

The first month is january 2021 and the last month can be the current one if possible.

 

Regards,

Cado

@Anonymous ,

 

No problem.

 

In Power Query, create a new blank query then paste this over the default code in Advanced Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvBx9AtRcFSK1YGxnZRiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [LOCATION = _t]),
    addSTART_DATE = Table.AddColumn(Source, "START_DATE", each List.Distinct(List.Transform({Number.From(Date.StartOfYear(DateTime.LocalNow()))..Number.From(Date.From(DateTime.LocalNow()))}, each Date.StartOfMonth(Date.From(_))))),
    expandSTART_DATE = Table.ExpandListColumn(addSTART_DATE, "START_DATE"),
    addEND_DATE = Table.AddColumn(expandSTART_DATE, "END_DATE", each Date.EndOfMonth([START_DATE])),
    chgTypes = Table.TransformColumnTypes(addEND_DATE,{{"START_DATE", type date}, {"END_DATE", type date}})
in
    chgTypes

 

You can now follow the steps I took to complete this.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Anonymous
Not applicable

@BA_Pete 

 

Thank you very much, I managed adapting your code to my query and it works perfectly !

 

Have a good day,

Cado

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors