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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

How to get the nearest date from a filtered table

I have a merged table with all my data with programmed purchases and date of purchase. I want to create a measure for the variance but first i need a variable that gets the nearest programmed purchase to the date of purchase.

IDDescriptionDate
CSProgrammed2/2/2023
CSPurchase2/2/2023
CSProgrammed3/10/2023
CSProgrammed5/9/2023
CSPurchase4/14/2023
CJProgrammed1/30/2023
CIProgrammed4/28/2023
CJPurchase1/30/2023
CJProgrammed6/9/2023
CJPurchase5/19/2023
CIPurchase5/19/2023
CIPurchase6/20/2023

I want that for each purchase the measure looks for the nearest programmed purchase for that ID so I can calculate the difference in days and then average by ID

1 ACCEPTED SOLUTION

Avg Nearest.pbix

 

Nothing too fancy,

ThxAlot_0-1692062683859.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



View solution in original post

7 REPLIES 7
Anonymous
Not applicable

For each purchase I want to look for the nearest programmed date for that ID, which means that for

CSPurchase

2/2/2023

the answer would be 

CSProgrammed2/2/2023

because its the nearest

but for 

CSPurchase4/14/2023

it would be 

CSProgrammed5/9/2023 

because that is nearest to 4/14/23 than 3/10/23.

and that calculation would be done for every purchase, then substract the date of purchase to the date programmed

so it would be 

=abs((2/2/23-2/2/23)+(4/14/23-5/9/23))=25

and average that by the amount of purchases

=25/2

=12.5 is the average of days between programmed dates and purchases dates

Avg Nearest.pbix

 

Nothing too fancy,

ThxAlot_0-1692062683859.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



Anonymous
Not applicable

I think it works great but what is the "@nearest" is it a variable? why isn't it at the beginning with var?

Hey @Anonymous ,

 

"@nearest" is only the name of a calculated column that is added by using ADDCOLUMNS to a virtual table, here this table is initially created by using SUMMARIZE.
The @ is often used by DAX developers to differentiate natural table columns from virtual columns.

Hopefully, this helps to understand @ThxAlot  great answer even better.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Anonymous
Not applicable

IDDescriptionDate
CSProgrammed2/2/2023
CSPurchase2/2/2023
CSProgrammed3/10/2023
CSProgrammed5/9/2023
CSPurchase4/14/2023
CJProgrammed1/30/2023
CJPurchase1/30/2023
CJProgrammed6/9/2023
CJPurchase5/19/2023
CIPurchase5/19/2023
CIPurchase6/20/2023
CIProgrammed4/28/2023
CIProgrammed6/21/2023

I added a line in the table for better understanding, but the answer would be...

IDAverage difference in days
CS12.5
CJ10.5
CI27

Hey @Anonymous,

 

you neither explained how the result is calculated, for more than ID, next, you also did not answer my question regarding your data model.

 

Take the time and answer the questions.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
TomMartens
Super User
Super User

Hey @Anonymous ,

 

describe the expected result based on the data in the above table.

Next, does your sample data reflect a table in a data model with a dedicated  calendar table or is your solution. based on a single table? 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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