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

Get Fabric Certified for FREE during AI Skills Fest. This week only. Secure your voucher now.

Reply
Anonymous
Not applicable

Sort by date and provide unique identifier

Hi,

 

My table consists of a range of activities with a huge range of start and end dates. I have written this piece of code to determine which activities are occurring in the 6 weeks (It seems to work but haven't done full testing):

 

if (((DateTime.IsInNextNHours([Start date], 1008) = true) or (DateTime.Date( DateTime.LocalNow() ) >= [Start date] and DateTime.Date( DateTime.LocalNow() ) <= [End date] )) and (DateTime.IsInNextNHours([End date], 1008) = true)) or ((DateTime.Date( DateTime.LocalNow() ) >= [Start date] and DateTime.Date( DateTime.LocalNow() ) <= [End date] )) then "6 week" else "out"

 

What I would ultimately like to do though is then give these activities a unique number that increases by 1, starting with the earliest Start date.

 

For example, the data would look something like:

ActivityStart dateEnd dateWithin 6 weeksUnique ID
Activity one8/3/202415/3/20246 week2
Activity two10/3/202210/4/2022outN/A
Activity three1/3/20243/3/20246 week1
Activity four17/3/202430/3/20246 week3

 

Some help would be wonderful!

 

Cheers,

K

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @Anonymous, try this:

 

Result:

dufoq3_0-1709203221741.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckwuySzLLKlUyM9LVdJRstA31jcyMDIBMg1NEWwzhfLU1GylWB0kDSXl+SBVBhBVRhC2CYydX1qCpjyjKBVkgyHCVGP8FqTllxaBNJgj6TDA1BILAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Activity = _t, #"Start date" = _t, #"End date" = _t, #"Within 6 weeks" = _t]),
    #"v1_Changed Type" = Table.TransformColumnTypes(Source,{{"Start date", type date}, {"End date", type date}}, "sk-SK"),
    #"v1_Added Index" = Table.AddIndexColumn(#"v1_Changed Type", "Index", 0, 1, Int64.Type),
    #"v1_Grouped Rows" = Table.Group(#"v1_Added Index", {"Within 6 weeks"}, {{"All", each Table.AddIndexColumn(Table.Sort(_, {"Start date", Order.Ascending}), "Unique ID", 1, 1, Int64.Type) , type table}}),
    v1_CombinedTables = Table.Combine(#"v1_Grouped Rows"[All]),
    v1_ReplaceValues = Table.ReplaceValue( v1_CombinedTables,
      each [Within 6 weeks],
      null,
      (x,y,z)=> if y = "out" then null else x,
      {"Unique ID"} ),
    #"v1_Sorted Rows" = Table.Sort(v1_ReplaceValues,{{"Index", Order.Ascending}}),
    #"v1_Removed Columns" = Table.RemoveColumns(#"v1_Sorted Rows",{"Index"})
in
    #"v1_Removed Columns"

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

1 REPLY 1
dufoq3
Super User
Super User

Hi @Anonymous, try this:

 

Result:

dufoq3_0-1709203221741.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckwuySzLLKlUyM9LVdJRstA31jcyMDIBMg1NEWwzhfLU1GylWB0kDSXl+SBVBhBVRhC2CYydX1qCpjyjKBVkgyHCVGP8FqTllxaBNJgj6TDA1BILAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Activity = _t, #"Start date" = _t, #"End date" = _t, #"Within 6 weeks" = _t]),
    #"v1_Changed Type" = Table.TransformColumnTypes(Source,{{"Start date", type date}, {"End date", type date}}, "sk-SK"),
    #"v1_Added Index" = Table.AddIndexColumn(#"v1_Changed Type", "Index", 0, 1, Int64.Type),
    #"v1_Grouped Rows" = Table.Group(#"v1_Added Index", {"Within 6 weeks"}, {{"All", each Table.AddIndexColumn(Table.Sort(_, {"Start date", Order.Ascending}), "Unique ID", 1, 1, Int64.Type) , type table}}),
    v1_CombinedTables = Table.Combine(#"v1_Grouped Rows"[All]),
    v1_ReplaceValues = Table.ReplaceValue( v1_CombinedTables,
      each [Within 6 weeks],
      null,
      (x,y,z)=> if y = "out" then null else x,
      {"Unique ID"} ),
    #"v1_Sorted Rows" = Table.Sort(v1_ReplaceValues,{{"Index", Order.Ascending}}),
    #"v1_Removed Columns" = Table.RemoveColumns(#"v1_Sorted Rows",{"Index"})
in
    #"v1_Removed Columns"

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

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.

Top Kudoed Authors