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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
BI_Prachi
Frequent Visitor

DAX to get totals based on condition of another column

Hi All,

I need help with Dax measure. Requirement is to get total quantities from Sales table only for those locations which exists in target table and transaction date of Sales should be greater than StartDate in Target Table.

SampleTables :
Targets:

LocationAreaRegionStartDateTarget
L01A1R105/06/2023100
L02A2R107/07/2023150


DimSite:

SiteKeyLocationAreaRegion
S1L01A1R1
S2L02A2R1
S3L03A1R1
S4L04A3R2

 

  Sales :

SiteKeyTransactionDateQty
S101/01/202340
S106/06/202330
S107/06/202340
S205/07/202360
S210/07/202350
S310/07/202340
S412/12/202370


Expected output is:

LocationQty
L170
L250



Relationship between tables are :
DimSite[Sitekey] ---> Sales[Sitekey] (one to many)
DimSite[LocationCode] <----> Target[LocationCode] (one- to-one)
DimDate[Date] -----> Sales[TransactionDate] (One-to-many)

Note :  Sales and dimSite table are being connected in DirectQueryMode as being loaded from exisisting power Bi model which cannot be changed and Target table in import mode which has target for limited locations.

I have created below measure which is giving correct output but taking ages to load on visual. 

CALCULATE(
    SUM(Sales[Quantity]),
    FILTER(
        SUMMARIZE(
            Sales,
            Sales[TransactionDate],
            'dim Site'[SiteKey],
            "GoLiveDate", CALCULATE(Max(StoreTarget[StartDate]), FILTER('dim Site', 'dim Site'[LocationCode]= VALUES(StoreTarget[LocationCode])))
),
        Sales[TransactionDate] >= [GoLiveDate]
    ),
    TREATAS(
        VALUES(StoreTarget[LocationCode]),
        'dim Site'[LocationCode]
    )
)


Thanks in advance!

2 REPLIES 2
v-nuoc-msft
Community Support
Community Support

Hi @BI_Prachi 

 

Please try the following:

 

Here's some dummy data

 

"Targets"

vnuocmsft_0-1709703902364.png

 

"Sales"

vnuocmsft_1-1709703922088.png

 

"DimSite"

vnuocmsft_2-1709703935520.png

 

Create a measure.

 

Qty = 
VAR TargetLocations = 
    CALCULATETABLE(
        VALUES(Targets[Location]),
        REMOVEFILTERS(DimSite)
    )
RETURN
    CALCULATE(
        SUM(Sales[Qty]),
        Sales[TransactionDate] > MIN(Targets[StartDate]),
        DimSite[Location] IN TargetLocations
    )

 

Here is the result.

vnuocmsft_3-1709704074290.png

 

Regards,

Nono Chen

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

 

Thanks @v-nuoc-msft 

I tried the solution. It's working fine at location level but totals are still not matching. As transaction date is being compared with min StartDate irrespective of StartDate of respective Location.

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.