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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
dkozul
New Member

Help with query to summarize stock data

Hello,

I am new to DAX so i need help.

I have table with stock data that is filled when stock quantity changes for example:

table.png

The logic should be like this:

- Take everything from that table where date is lower or equal to selected date.

- Group by PharmacyId and ProductId and for every group take quantity with biggest date

- Sum quantites from that result table.

 

Like you see in my example for some dates i have data for some i don't. For that that i don't it should take last avaliable.

 

I wrote two mesasures:

1. StockCountRows - first just returns number of rows from table that i calculate to see if that part is correct:

    COUNTROWS(CALCULATETABLE(StockItemTest; FILTER(ALL(DimVrijeme); DimVrijeme[Datum] <= MAX(DimVrijeme[Datum]))))

2. StockQuantity - second is the main measure which contains the first one:

SUMX(

    ADDCOLUMNS(

        SUMMARIZE(

            CALCULATETABLE(StockItemTest; FILTER(ALL(DimVrijeme); DimVrijeme[Datum] <= MAX(DimVrijeme[Datum])));

            StockItemTest[ProductId];

            StockItemTest[PharmacyId];

            "MaxDate"; MAX(StockItemTest[Date] )

        );

    "QuantityPerGroup"; CALCULATE(SELECTEDVALUE(StockItemTest[Quantity]); StockItemTest[Date] = EARLIER([MaxDate]))

    ); [QuantityPerGroup]

)

 

Everything is fine when i choose a month for which i have data, let's say September:

result1.png

But when i choose August i get blank:

result2.png

 

I need help because i don't know what to do next.

 

Thanks

1 ACCEPTED SOLUTION
v-chuncz-msft
Community Support
Community Support

@dkozul 

 

You may change it as follows.

        "QuantityPerGroup", CALCULATE (
            SELECTEDVALUE ( StockItemTest[Quantity] ),
            StockItemTest[Date] = EARLIER ( [MaxDate] ),
            ALL ( DimVrijeme[Datum] )
        )

 

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-chuncz-msft
Community Support
Community Support

@dkozul 

 

You may change it as follows.

        "QuantityPerGroup", CALCULATE (
            SELECTEDVALUE ( StockItemTest[Quantity] ),
            StockItemTest[Date] = EARLIER ( [MaxDate] ),
            ALL ( DimVrijeme[Datum] )
        )

 

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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