Forum Discussion

jimitko's avatar
jimitko
Frequent Visitor
3 years ago
Solved

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.

 

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

  • 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])
    )

3 Replies

  • 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")

     

     

  • v-zhangti's avatar
    v-zhangti
    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

    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.

     

  • jimitko's avatar
    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])
    )