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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Patrick_PowerBI
Frequent Visitor

Need Help, state on the first day of year

I have an table where an item can have a different status over time. Now I like to have the status on the first day of the year.

IDstartdateenddateEPA label
12015-10-01 00:00:00.0002016-05-12 00:00:00.000D
12016-05-13 00:00:00.0002016-06-30 00:00:00.000D
12016-07-01 00:00:00.0002017-03-14 00:00:00.000D
12017-03-15 00:00:00.0002017-06-30 00:00:00.000D
12017-07-01 00:00:00.0002018-02-12 00:00:00.000D
12018-02-13 00:00:00.0002018-02-13 00:00:00.000 
12018-02-14 00:00:00.0002018-06-30 00:00:00.000D
12018-07-01 00:00:00.0002018-07-01 00:00:00.000D
12018-07-01 00:00:00.0002018-12-10 00:00:00.000D
12018-12-11 00:00:00.0002018-12-11 00:00:00.000D
12018-12-11 00:00:00.0002018-12-11 00:00:00.000D
12018-12-11 00:00:00.0002019-06-30 00:00:00.000D
12019-07-01 00:00:00.0002019-07-01 00:00:00.000D
12019-07-01 00:00:00.0002019-10-13 00:00:00.000D
12019-10-14 00:00:00.0002020-06-30 00:00:00.000D
12020-07-01 00:00:00.0002020-07-01 00:00:00.000D
12020-07-01 00:00:00.0002020-07-01 00:00:00.000D
12020-07-01 00:00:00.0009999-12-31 00:00:00.000D
22015-10-01 00:00:00.0002016-05-12 00:00:00.000D
22016-05-13 00:00:00.0002016-06-30 00:00:00.000D
22016-07-01 00:00:00.0002017-03-14 00:00:00.000D
22017-03-15 00:00:00.0002017-06-30 00:00:00.000E
22017-07-01 00:00:00.0002018-02-12 00:00:00.000E
22018-02-13 00:00:00.0002018-06-30 00:00:00.000E
22018-07-01 00:00:00.0002018-07-01 00:00:00.000E
22018-07-01 00:00:00.0002019-06-30 00:00:00.000E
22019-07-01 00:00:00.0002019-07-01 00:00:00.000E
22019-07-01 00:00:00.0002020-06-30 00:00:00.000E
22020-07-01 00:00:00.0002020-07-01 00:00:00.000E

 

 

In T-SQL I would do something like:

SELECT
ID,
startdate,
enddate,
[EPA label]
FROM [table]
WHERE '2016-01-01' between startdate and enddate
union
SELECT
ID,
startdate,
enddate,
[EPA label]
FROM [table]
WHERE '2017-01-01' between startdate and enddate
ORDER BY
ID,
startdate
etc.

 

To get:

 

IDstartdateenddateEPA label
12015-10-01 00:00:00.0002016-05-12 00:00:00.000D
12016-07-01 00:00:00.0002017-03-14 00:00:00.000C
22015-10-01 00:00:00.0002016-05-12 00:00:00.000D
22016-07-01 00:00:00.0002017-03-14 00:00:00.000A
32015-10-01 00:00:00.0002016-05-12 00:00:00.000D
32016-07-01 00:00:00.0002017-03-14 00:00:00.000D
42015-10-01 00:00:00.0002016-05-12 00:00:00.000D
42016-07-01 00:00:00.0002017-03-14 00:00:00.000A
52015-10-01 00:00:00.0002016-05-12 00:00:00.000D
52016-07-01 00:00:00.0002017-03-14 00:00:00.000E

 

How do I do this in Power-BI in either M or Dax?

I can use what I made and make it in M but are there better ways?

thanks!

1 ACCEPTED SOLUTION
AlB
Community Champion
Community Champion

Hi @Patrick_PowerBI 

You can do it in M or DAX. Here is an M option. Place the following M code in a blank query to see the steps.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("tZTdDoMgDIVfxXBtl1Lm3663pzC+/2sMRrJNaCloNF55+uW0YM+6Gmt6Q2gHsAhoO8TH570hYlRGQC9SqjzN1n/pWON4egSHGj3x3v6zA3sv0rFm4GnVexK9Z0DS5o413NyC0uV0Nl1U1M7nUuecUk1b35XmHWpkWvO+hl4qTm0R5xaUatrvT37fCR1qmPsm1DsPNby3pFxPL/4Jt+EEmk5lC53Klh99JFvoYLa8ErotW3Z0MVtU7+Z0qKSFHdvRzTtWR0tb8k+3/+ee3t4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, startdate = _t, enddate = _t, #"EPA label" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"startdate", type datetime}, {"enddate", type datetime}, {"EPA label", type text}}),


    years_ = List.Distinct(List.Transform(List.Combine({#"Changed Type"[startdate], #"Changed Type"[enddate]} ), each Date.Year(_))),
    firstDays_ = List.Transform(years_, each DateTime.From(Text.From(_) & "-01-01")), 
    res_ = Table.SelectRows(#"Changed Type", each List.AnyTrue(List.Transform(firstDays_, (inner)=> [startdate] <= inner and inner<=[enddate]))) 
in
    res_

 The 3 last steps are the important ones

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

 

View solution in original post

2 REPLIES 2
AlB
Community Champion
Community Champion

Hi @Patrick_PowerBI 

You can do it in M or DAX. Here is an M option. Place the following M code in a blank query to see the steps.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("tZTdDoMgDIVfxXBtl1Lm3663pzC+/2sMRrJNaCloNF55+uW0YM+6Gmt6Q2gHsAhoO8TH570hYlRGQC9SqjzN1n/pWON4egSHGj3x3v6zA3sv0rFm4GnVexK9Z0DS5o413NyC0uV0Nl1U1M7nUuecUk1b35XmHWpkWvO+hl4qTm0R5xaUatrvT37fCR1qmPsm1DsPNby3pFxPL/4Jt+EEmk5lC53Klh99JFvoYLa8ErotW3Z0MVtU7+Z0qKSFHdvRzTtWR0tb8k+3/+ee3t4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, startdate = _t, enddate = _t, #"EPA label" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"startdate", type datetime}, {"enddate", type datetime}, {"EPA label", type text}}),


    years_ = List.Distinct(List.Transform(List.Combine({#"Changed Type"[startdate], #"Changed Type"[enddate]} ), each Date.Year(_))),
    firstDays_ = List.Transform(years_, each DateTime.From(Text.From(_) & "-01-01")), 
    res_ = Table.SelectRows(#"Changed Type", each List.AnyTrue(List.Transform(firstDays_, (inner)=> [startdate] <= inner and inner<=[enddate]))) 
in
    res_

 The 3 last steps are the important ones

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

 

Thanks this is a very very nice solution 👍

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.