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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
daxnebie
Regular Visitor

Find Nearest Older Date and Sum the Amount

Hi Guys, so I have two simple tables:

 

DimTable Discount:

DateAmount
2019-01-0110%
2020-01-0120%
2021-01-0130%

 

FactTable Sales:

DateAmount
2020-01-0220

 

So the idea is to find the nearest older date based on the date of FactTable Sales, so step-by-step it would be like this:

FactTable SalesFactTable SalesDimTable DiscountDimTable DiscountMeasures
DateAmountDateAmountMeasures
2020-01-02202020-01-0120%16

 

20 * (1-20%) = 16

 

The end product is bar chart compose of full price and discount price, per year. 

 

I understand that it's a very simple dataset, but the real data is more complicated than this. The discount rate changes daily.

 

Both tables are linked to a Date Table.

 

I have tried measures and calculated column but nothing works. The measure always return 10% instead of 20%. Do you guys can help me? 

 

My code for calculated column is below and this return 2019 which is 10%, instead of 2020, which is 20%.

 

SurveyDate = 
CALCULATE ( 
    MIN(Discount[Date]),
   FILTER ( 
        Discount,
        Discount[date] >= related(Sales[date])
    ) 
)

 

 

For measures, I even tried to use summarizecolumns, but that doesn't work either because of the filtering issues which I still do not understand why.

 

Thanks guys for your help! Really appriciate it

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@daxnebie , Create a new column like

 

new column =
var _max = maxx(filter(discount, discount[Date]<= Sales[DAte]), discount[date])
var _min = minx(filter(discount, discount[Date]> Sales[DAte]), discount[date])
var _diff1 = datediff(_max, ,Sales[DAte], day)
var _diff2 = datediff(Sales[DAte],_min, day)
var _date = Switch(true() , _diff1 > _diff2, _min , _max)
return
sales[Amount] * (1- maxx(filter(discount, discount[Date]= _date), discount[Amount]))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

2 REPLIES 2
tamerj1
Super User
Super User

Hi @daxnebie 

I undestand that your problem comes from the gaps in dates in the dim. table. 
in this case the I would suggest to workt out in power query. You can easily create a date table in power query from first date to last date on the fact table. Then you can merge the dim table with the date table (left outer join). Keep only the complete dates column and then "down fill" the Amount column. 

amitchandak
Super User
Super User

@daxnebie , Create a new column like

 

new column =
var _max = maxx(filter(discount, discount[Date]<= Sales[DAte]), discount[date])
var _min = minx(filter(discount, discount[Date]> Sales[DAte]), discount[date])
var _diff1 = datediff(_max, ,Sales[DAte], day)
var _diff2 = datediff(Sales[DAte],_min, day)
var _date = Switch(true() , _diff1 > _diff2, _min , _max)
return
sales[Amount] * (1- maxx(filter(discount, discount[Date]= _date), discount[Amount]))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.