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
jimitko
Frequent Visitor

Filter values by date range from table

Hi all,

 

I'm pretty stuck with the following issue:

 

I do have one table with Orders (Order No., Order Date, Customer No., Value) and a second table with Customers (No., Valid since, Valid Until.). Valid since and valid Until is saying when "Value of order" should be counted into Value with date filtering.

jimitko_0-1692369078905.png

 

Those tables are connected with M:1 by Customer No. 

 

Example of customers

Customer No.Valid sinceValid until
101.01.202310.01.2023
201.01.202310.02.2023
301.02.202330.03.2023
415.01.202330.03.2023
501.01.202330.03.2023
601.01.202330.03.2023
120.01.202330.01.2023

 

Example of Orders            

Order No.Order DateValue           Customer No.
101.01.2023551
202.01.202360232
303.01.2023116513
404.01.2023164
515.01.2023151
616.01.2023161
707.01.202367
808.01.20231581
929.01.202312
1030.01.2023511

 

What I wanna do is apply the rule that should count the Value of orders only when the customer was valid and there is one more condition that one customer can be valid twice or more. In the provided example I wanna count sales for customer 1 only between dates 01.01.-10.01.2023 and 20.01.-30.01.2023. So the result for customer 1 should be counting orders 1, 8, and 10 with result Value 264 (55 + 158 + 51) and not count orders 5 and 6 as these are not in a valid period.

 

I tried to use the following Measure:

Value with date filtering = SUMX(FILTER(orders, orders[Order Date]>=MIN(customers[Valid since]) && orders[Order Date]<= MAX(customers[Valid until])),[Value])

 

 ... but without success.

 

Could someone please help me?

 

Thanks.

 

Jakub

1 ACCEPTED SOLUTION
jimitko
Frequent Visitor

Actually, ChatGPT provided me the correct answer code after a few attempts:

 

Valid Sales = 
SUMX(
    customers,
    VAR CurrentCustomer = customers[No]
    VAR ValidPeriods = 
        FILTER(
            orders,
            orders[Customer No.] = CurrentCustomer &&
            orders[Order Date] >= customers[Valid since] &&
            orders[Order Date] <= customers[Valid until]
        )
    RETURN
        SUMX(ValidPeriods, orders[Value])
)

View solution in original post

3 REPLIES 3
jimitko
Frequent Visitor

Actually, ChatGPT provided me the correct answer code after a few attempts:

 

Valid Sales = 
SUMX(
    customers,
    VAR CurrentCustomer = customers[No]
    VAR ValidPeriods = 
        FILTER(
            orders,
            orders[Customer No.] = CurrentCustomer &&
            orders[Order Date] >= customers[Valid since] &&
            orders[Order Date] <= customers[Valid until]
        )
    RETURN
        SUMX(ValidPeriods, orders[Value])
)
v-zhangti
Community Support
Community Support

Hi, @jimitko 

 

You can try the following methods.

Measure =
VAR _N1 = CALCULATE ( SUM ( Orders[Value] ),
        FILTER ( ALL ( Orders ),
            [Order Date] >= MIN ( Customers[Valid since] )
                && [Order Date] <= MIN ( Customers[Valid until] )
                && [Customer No.] = SELECTEDVALUE ( Customers[Customer No.] )
        )
    )
VAR _N2 = CALCULATE ( SUM ( Orders[Value] ),
        FILTER ( ALL ( Orders ),
            [Order Date] >= MAX ( Customers[Valid since] )
                && [Order Date] <= MAX ( Customers[Valid until] )
                && [Customer No.] = SELECTEDVALUE ( Customers[Customer No.] )
        )
    )
RETURN
    _N1 + _N2

vzhangti_0-1692965483983.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

amitchandak
Super User
Super User

@jimitko , Create a valid column flag in you table and use that in meausre

 

flag =

var _cnt = Countrows(FILTER(orders, orders[Customer No]=(customers[no]) && orders[Order Date]>=(customers[Valid since]) && orders[Order Date]<= (customers[Valid until])))

return

if(isblank(_cnt),"Not Valid", "Valid")

 

 

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.

Top Solution Authors