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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
jirim
Frequent Visitor

Transform date range to period range

Hello exprerts

 

My datasource is a s following:

 

Date fromDate toShare
01.01.202015.03.202070
16.03.202015.08.202150
01.12.202131.01.202250
01.02.202231.05.202260
01.06.202230.06.202260
01.07.202231.08.202260
01.10.202231.12.9999100

 

Ie. date validity ranges for "Share" that do not overlap but can have "holes"

 

I need to transorm the above to period (month) ranges. If there are more than one value for the month the last one is used. If the subsequent ranges have same "Share" they should be joined.

 

For data as above the expected result is following

Period fromPeriod toShare
20200120200270
20200320210850
20211220220150
20220220220860
202210999912100

 

I have already spent several hours trying to figure out the M-code for the transformation but failed.

 

Can anyone help?

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

A showcase of powerful Table.Group(),

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc7RDcAgCATQXfg25sCgdhbj/msUYmhpavi5y5OwFoGrjUBAhVgrWoQB2mUR99Q5mB7Ygh5g31mia7FOPgASnQON0F/QH4AUEhh5w/wDRgJ20GXPL4aJfQM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Date from" = _t, #"Date to" = _t, Share = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date from", type date}, {"Date to", type date}, {"Share", Int64.Type}}, "fr"),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index"),
    Grouped =
        let rows = Table.ToRecords(#"Added Index") in Table.Group(#"Added Index", Table.ColumnNames(#"Added Index"), {"grp", each let rs = Table.ToRecords(_) in [From=Date.ToText(rs{0}[Date from],"yyyyMM"), To=Date.ToText(List.Last(rs)[Date to],"yyyyMM"), Share=rs{0}[Share]]}, 0, (x,y) => Byte.From(let r = rows{y[Index]-1} in y[Date from] <> r[Date to] + #duration(1,0,0,0) or y[Share] <> r[Share])),
    #"New Table" = Table.FromRecords(Grouped[grp])
in
    #"New Table"

CNENFRNL_0-1654712804900.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

3 REPLIES 3
CNENFRNL
Community Champion
Community Champion

A showcase of powerful Table.Group(),

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc7RDcAgCATQXfg25sCgdhbj/msUYmhpavi5y5OwFoGrjUBAhVgrWoQB2mUR99Q5mB7Ygh5g31mia7FOPgASnQON0F/QH4AUEhh5w/wDRgJ20GXPL4aJfQM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Date from" = _t, #"Date to" = _t, Share = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date from", type date}, {"Date to", type date}, {"Share", Int64.Type}}, "fr"),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index"),
    Grouped =
        let rows = Table.ToRecords(#"Added Index") in Table.Group(#"Added Index", Table.ColumnNames(#"Added Index"), {"grp", each let rs = Table.ToRecords(_) in [From=Date.ToText(rs{0}[Date from],"yyyyMM"), To=Date.ToText(List.Last(rs)[Date to],"yyyyMM"), Share=rs{0}[Share]]}, 0, (x,y) => Byte.From(let r = rows{y[Index]-1} in y[Date from] <> r[Date to] + #duration(1,0,0,0) or y[Share] <> r[Share])),
    #"New Table" = Table.FromRecords(Grouped[grp])
in
    #"New Table"

CNENFRNL_0-1654712804900.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Absolutely amazing. It works great. But I am not sure if my mental capacity is capable to understand how 🙂

 

Thanks a lot

Anonymous
Not applicable

This is cool, what UI steps equal to Table.Group() ? 

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.