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

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

Reply
Anonymous
Not applicable

Reference Data Given Filters Max Date & Date Less than Other Date

Hello, I have a large dataset of Items purchased by Date, and Item Costing Implementation by Date. I am having trouble pulling the cost at the time of purchase into the purchase table. My datasets are below. I also have a Date Table as a bridge.

ItemDate PurchasedQty Purchased ItemDate of Cost ImplementationItem Cost
11/1/201920 15/4/201710
13/4/2019100 16/9/201811
17/3/201965 14/23/201912
22/5/201940 212/9/201820
26/16/201975 23/18/201921
28/23/201935 34/23/201930
33/4/2019110 39/2/201931
38/4/201920    
312/5/201965    

Could someone please help me with the logic on how to pull the cost (at the time of purchase) into the purchase table in the "Cost of Item" column?

 

Below is my desired output for Cost of Item in this sample set. I have also provided a link to a pbix file for this.

ItemDate PurchasedQty PurchasedCost of Item
11/1/201920$11
13/4/2019100$11
17/3/201965$12
22/5/201940$20
26/16/201975$21
28/23/201935$21
33/4/2019110$30
38/4/201920$30

Example File 

2 ACCEPTED SOLUTIONS
Greg_Deckler
Community Champion
Community Champion

Perhaps:

 

Cost of Item = 
  VAR __ItemCostDate =
    MAXX(
      FILTER('CostTable',[Date of Cost Implmementation] <= [Date Purchased] && 'CostTable'[Item] = 'PurchaseTable'[Item]),
      [Date of Cost Implementation]
    )
RETURN
  MAXX(
    FILTER('CostTable',[Date of Cost Implementation] = __ItemCostDate && 'CostTable'[Item]='PurchaseTable'[Item]),
    [Item Cost]
  )


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

Hi @Anonymous  

this might be a faster operation in the query editor:

let
    Source = CostTable & PurchaseTable,
    #"Duplicated Column" = Table.DuplicateColumn(Source, "Cost of Item", "Cost of Item - Copy"),
    #"Sorted Rows" = Table.Sort(#"Duplicated Column",{{"Item", Order.Ascending}, {"Date Purchased", Order.Ascending}, {"Cost of Item", Order.Descending}}),
    #"Filled Down" = Table.FillDown(#"Sorted Rows",{"Cost of Item"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([#"Cost of Item - Copy"] = null))
in
    #"Filtered Rows"

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

7 REPLIES 7
Greg_Deckler
Community Champion
Community Champion

Perhaps:

 

Cost of Item = 
  VAR __ItemCostDate =
    MAXX(
      FILTER('CostTable',[Date of Cost Implmementation] <= [Date Purchased] && 'CostTable'[Item] = 'PurchaseTable'[Item]),
      [Date of Cost Implementation]
    )
RETURN
  MAXX(
    FILTER('CostTable',[Date of Cost Implementation] = __ItemCostDate && 'CostTable'[Item]='PurchaseTable'[Item]),
    [Item Cost]
  )


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler 
Thanks for the quick response & solution!!!

Anonymous
Not applicable

@Greg_Deckler 

 

I'm sad to re-post in this, but I have an issue. This calculation takes FOREVER to run with my data. Currently, I am asking to reference a table with 2.9M rows & pull data from that to a table with 94k rows. Unfortunately, I need to run this for 5 columns. 

Do I need to just accept that these will take all day, or is there a better way using either Measures or Edit Queries?

 

Ryan

Well, DAX optimization is a long and complex subject. I'd have to think about whether this can be done more efficiently. Would protentially help to move it to Power Query, perhaps @ImkeF  has some suggestions there.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hi @Anonymous  

this might be a faster operation in the query editor:

let
    Source = CostTable & PurchaseTable,
    #"Duplicated Column" = Table.DuplicateColumn(Source, "Cost of Item", "Cost of Item - Copy"),
    #"Sorted Rows" = Table.Sort(#"Duplicated Column",{{"Item", Order.Ascending}, {"Date Purchased", Order.Ascending}, {"Cost of Item", Order.Descending}}),
    #"Filled Down" = Table.FillDown(#"Sorted Rows",{"Cost of Item"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([#"Cost of Item - Copy"] = null))
in
    #"Filtered Rows"

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

@ImkeF @Greg_Deckler 

 

Thanks so much to each of you. One question. 

 

Is there a way to write DAX code for multiple columns or measures before actually processing the code? ie, I could create each column at end of day before leaving work & having it run overnight.

 

Thanks!

Ryan

In thinking about this, I really think this comes down to your data model in terms of the speed of processing. If you could create a table that filled in all of the dates in your Cost table for your products that would speed things up tremendously because then you could create a relationship or use LOOKUPVALUE to grab your cost and avoid the messiness of table scanning with iteration functions like MAXX, etc.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

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!

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.