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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

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
Anonymous
Not applicable

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 @Anonymous 

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
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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