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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply

How to add Index Column based on date condition

Hello All,

 

I have a below table

DateNumber
2/3/2023 12:00:00 AM160
4/14/2023 12:00:00 AM161
5/16/2023 12:00:00 AM162

6/23/2023 12:00:00 AM

 

163
7/23/2023 12:00:00 AM164
9/26/2023 12:00:00 AM165
10/20/2023 12:00:00 AM166
11/3/2023 12:00:00 AM167

 

I need to create an index starting from 1 if [Date]>today() otherwise negative index or null values.

 

Expected Result:

 

DateNumberIndexIndex
2/3/2023 12:00:00 AM160null-4
4/14/2023 12:00:00 AM161null-3
5/16/2023 12:00:00 AM162null-2
6/23/2023 12:00:00 AM163null-1
7/23/2023 12:00:00 AM164null0
9/26/2023 12:00:00 AM16511
10/20/2023 12:00:00 AM16622
11/3/2023 12:00:00 AM16733

 

Tried below logic but its creating 53 rows for each date starting 9/26/2023 and adding index from 1 to 53 for each date and repeating that pattern for all dates starting from 9/26/2023.

 

Index= Table.AddColumn(#"Changed Type", "Custom", each if [Date]>DateTime.LocalNow() then Table.AddIndexColumn(#"Changed Type", "Index", 1, 1,Int64.Type) else null)

 

Can you please let me know how to achieve the above result. I am able to achieve this using dax but I need this in Query editor not in DAX.

 

Thank you!

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@ashuaswinireddy here is the output

 

parry2k_0-1691705126637.png

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

2 REPLIES 2
parry2k
Super User
Super User

@ashuaswinireddy here is the output

 

parry2k_0-1691705126637.png

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@ashuaswinireddy here is one way, start a blank query -> advanced editor and paste the M code below, change it as you see it.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc87CoAwEEXRrYSpA/PJD+0EW91ASKGQ0kZ0/4axTWC6c3kwOcN6PBUs7O911huKzSDoUEicYZmJ2pllawVHUvbIvu+sHpBj30W94WDfqaehe/UJZbAf1Jma9oP4Bzx6MEEpHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type datetime}, {"Number", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each if [Date]>DateTime.LocalNow() then "More" else "Less"),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Custom"}, {{"All", each _, type table [Date=nullable datetime, Number=nullable number, Custom=text]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Custom.1", each if  [Custom] = "Less" then Table.AddIndexColumn([All],"Index" ,Table.RowCount([All])*-1,1) else Table.AddIndexColumn([All], "Index", 0,1)),
    #"Expanded Custom.1" = Table.ExpandTableColumn(#"Added Custom1", "Custom.1", {"Date", "Number", "Index"}, {"Date", "Number", "Index"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom.1",{"Custom", "All"})
in
    #"Removed Columns"

 

👉 Learn Power BI and Fabric - subscribe to our YT channel - @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤️



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.

Top Solution Authors
Top Kudoed Authors