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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

Need help with DAX: Calculating % from latest store visits

Hi All

 

I am working on a Power BI report which requires me to calculate the % of a product quantity at a store for the latest visit dynamically based on the store level. For example, 

 

For store C and product X, the latest visit is on 25/4 and hence the calculation should be (2/335)*100=0.59%.

 

I tried writing DAX, but failed miserably in going further ahead. This is what I wrote-

 

QuantityX% = var numerator=SUM(QuantityX) var denominator=SUM(TotalStoreQuantity)
return CALCULATE(DIVIDE(numerator,denominator), Latest(Date))
 
The final % should be the %quantity of product X from all stores(only latest visits). Similarly, when a user selects product Y, the same logic repeats.
The data sample is below-
StoreProductDateQuantity of XTotal quantity of all items in store
AX25/04/20192289
BX17/04/20191360
BX30/04/20191377
CX04/04/20192467
CX25/04/20192335
DX12/04/20191519
DX12/04/20191519
EX11/04/20191499
EX11/04/20191499
FX01/04/20191302
GX18/04/20191226
HX19/04/20192596
IX12/04/20192472
JX30/04/20192245
KX23/04/20191430
LX05/04/20192439
MX25/04/20193644
WWY23/07/2019124
SY10/07/20193353
SY24/07/20193369
QY19/07/20194191
AZY11/07/20194339
ZZY16/07/20191459
QDY05/07/20191214
HGY09/07/20192899
RTY24/07/20191337
DSY30/07/20192224
SSY23/07/20193502
MMY05/07/20193407
FFY31/07/2019115
KKY31/07/2019168
EFY08/07/20192241
DFY12/07/2019281
AwY22/07/2019166
 
Any help would be highly appreciated!
1 ACCEPTED SOLUTION
Anonymous
Not applicable

The following calculation should help:

 

Product % = 
VAR varLastDate =
    CALCULATE ( MAX ( 'Data'[Date] ) )
RETURN
    IF (
        HASONEVALUE ( Data[Product] ) && HASONEVALUE ( 'Data'[Store] ),
        CALCULATE (
            DIVIDE (
                SUM ( Data[Quantity of X] ),
                SUM ( 'Data'[Total quantity of all items in store] )
            ),
            FILTER (
                ALLEXCEPT ( 'Data', 'Data'[Product], 'Data'[Store] ),
                'Data'[Date] = varLastDate
            )
        )
    )

2019-08-08_19h48_48.png

I've also created a sample workbook you can download here: Sample Workbook

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Thank you so much! Works like a charm.

Anonymous
Not applicable

The following calculation should help:

 

Product % = 
VAR varLastDate =
    CALCULATE ( MAX ( 'Data'[Date] ) )
RETURN
    IF (
        HASONEVALUE ( Data[Product] ) && HASONEVALUE ( 'Data'[Store] ),
        CALCULATE (
            DIVIDE (
                SUM ( Data[Quantity of X] ),
                SUM ( 'Data'[Total quantity of all items in store] )
            ),
            FILTER (
                ALLEXCEPT ( 'Data', 'Data'[Product], 'Data'[Store] ),
                'Data'[Date] = varLastDate
            )
        )
    )

2019-08-08_19h48_48.png

I've also created a sample workbook you can download here: Sample Workbook

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.