cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Reply
Baskar
Resident Rockstar
Resident Rockstar

Help : DAX optimization

Thanks Advanced !!!

 

This is my DAX measure to calculated the sales made >4500 with in 3 transaction. 

Here am getting expected result but the measure perfomance is very poor. 

 

Test Measure =
VAR _curdate =
    MAX ( 'Date'[Date] )
VAR _prevdate =
    DATE ( YEAR ( _curdate ), MONTH ( _curdate ) - 12, DAY ( _curdate ) )
VAR _table =
    CALCULATE (
        COUNTROWS (
            FILTER (
                SUMMARIZE (
                    Sales,
                    Sales[LoyaltyId],
                    Sales[Bill Flag],
                    "BuyingVal", SUM ( Sales[NetSaleAmount] ),
                    "BuyingFreq", DISTINCTCOUNT ( Sales[Original Bill Num] )
                ),
                Sales[Bill Flag] = 1
                && [BuyingVal] > 4500
                && [BuyingFreq] < 3
            )
        ),
        FILTER ( ALL ( 'Date' ), 'Date'[Date] > _prevdate && 'Date'[Date] <= _curdate )
    )
RETURN
    _table

 

 

Help me to come over from here. 
@ImkeF @parry2k @Zubair_Muhammad @Ashish_Mathur @Greg_Deckler @MFelix @MattAllington 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

@Baskar  - The following will give you 80 for Jan 2015 in your file:

Checking 2 = 
var _cur = MAX('Date Master'[Start Date])
var _pre = DATE(YEAR(_cur),MONTH(_cur)-12,DAY(_cur))
var _filter = 
CALCULATETABLE(
    Orders, 
    Orders[Ship Mode]="Standard Class",
    FILTER(ALL('Date Master'),'Date Master'[Start Date] >_pre && 
            'Date Master'[Start Date] <= _cur))
var _summed = 
SUMMARIZE(
    _filter,
    [Customer ID],
    "sales",SUM([Sales]),
    "count",DISTINCTCOUNT([Order ID])                       
)
return  COUNTROWS(filter(_summed,[sales] >1000 && [count] <5))
I hope this helps. If it does, please Mark as a solution.
I also appreciate Kudos.
Nathan Peterson

View solution in original post

15 REPLIES 15
Zubair_Muhammad
Community Champion
Community Champion

@Baskar 

 

Try this one

 

Test Measure =
VAR _curdate =
    MAX ( 'Date'[Date] )
VAR _prevdate =
    DATE ( YEAR ( _curdate ), MONTH ( _curdate ) - 12, DAY ( _curdate ) )
VAR filtered_table =
    FILTER ( Sales, Sales[Bill Flag] = 1 )
VAR _table =
    CALCULATE (
        COUNTROWS (
            FILTER (
                SUMMARIZE (
                    filtered_table,
                    [LoyaltyId],
                    [Bill Flag],
                    "BuyingVal", SUM ( Sales[NetSaleAmount] ),
                    "BuyingFreq", DISTINCTCOUNT ( Sales[Original Bill Num] )
                ),
                [BuyingVal] > 4500
                    && [BuyingFreq] < 3
            )
        ),
        FILTER ( ALL ( 'Date' ), 'Date'[Date] > _prevdate && 'Date'[Date] <= _curdate )
    )
RETURN
    _table

Regards
Zubair

Please try my custom visuals

Hi @Zubair_Muhammad 

Thanks for your response my friend. 

 

While using your code am not getting expected result.

While using the var 

VAR filtered_table =
    FILTER ( Sales, Sales[Bill Flag] = 1 )

This condition is not working 

 

  FILTER ( ALL ( 'Date' ), 'Date'[Date] > _prevdate && 'Date'[Date] <= _curdate )

Does this work better?:

 

Test Measure =
VAR _curdate =
    MAX ( 'Date'[Date] )
VAR _prevdate =
    DATE ( YEAR ( _curdate ), MONTH ( _curdate ) - 12, DAY ( _curdate ) )
VAR filtered_table =
    CALCULATETABLE (
        FILTER ( Sales, Sales[Bill Flag] = 1 ),
        FILTER ( ALL ( 'Date' ), 'Date'[Date] > _prevdate && 'Date'[Date] <= _curdate )
    )
VAR _table =
    COUNTROWS (
        FILTER (
            ADDCOLUMNS (
                SUMMARIZE ( filtered_table, [LoyaltyId], [Bill Flag] ),
                "BuyingVal", SUM ( Sales[NetSaleAmount] ),
                "BuyingFreq", DISTINCTCOUNT ( Sales[Original Bill Num] )
            ),
            [BuyingVal] > 4500
                && [BuyingFreq] < 3
        )
    )
RETURN
    _table

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Baskar
Resident Rockstar
Resident Rockstar

Thanks @ImkeF 

 

While using your code am getting unexpected results ( getting less count ).

 

Still am helpless

How about the performance?

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Baskar
Resident Rockstar
Resident Rockstar

Better then previous. It is great.
But result completly demaged 🙂

I would suggest to check all filters or split it up into more variables to check. I might have typos in my code, as I couldn't test the code.

Maybe these functions will help you as well: https://www.thebiccountant.com/2019/07/04/debug-dax-variables-in-power-bi-and-power-pivot/

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Sorry, my bad, forgot the CALCULATE in the ADDCOLUMNS:

 

Test Measure =
VAR _curdate =
    MAX ( 'Date'[Date] )
VAR _prevdate =
    DATE ( YEAR ( _curdate ), MONTH ( _curdate ) - 12, DAY ( _curdate ) )
VAR filtered_table =
    CALCULATETABLE (
        FILTER ( Sales, Sales[Bill Flag] = 1 ),
        FILTER ( ALL ( 'Date' ), 'Date'[Date] > _prevdate && 'Date'[Date] <= _curdate )
    )
VAR _table =
    COUNTROWS (
        FILTER (
            ADDCOLUMNS (
                SUMMARIZE ( filtered_table, [LoyaltyId], [Bill Flag] ),
                "BuyingVal", CALCULATE ( SUM ( Sales[NetSaleAmount] ) ),
                "BuyingFreq", CALCULATE ( DISTINCTCOUNT ( Sales[Original Bill Num] ) )
            ),
            [BuyingVal] > 4500
                && [BuyingFreq] < 3
        )
    )
RETURN
    _table

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Baskar
Resident Rockstar
Resident Rockstar

Thanks @ImkeF , FYI I have already added calculate in addcolumn earlier. No luck

I don't see what else I can do without any data to compare against here.

So you might consider creating some sample data with source and desired outcome so we can narrow down the problem.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Baskar
Resident Rockstar
Resident Rockstar

@ImkeF  Here i have attached power bi file with sample data. 

 

Example : 

For  Jan 2015 expected result is 80, but we are getting 3. 

 

Refer the first tab for raw data. 

THanks, but I cannot spot the measure that returns the correct figures.

What's its name?  - and if it isn't included, please include it as well.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

@Baskar  - The following will give you 80 for Jan 2015 in your file:

Checking 2 = 
var _cur = MAX('Date Master'[Start Date])
var _pre = DATE(YEAR(_cur),MONTH(_cur)-12,DAY(_cur))
var _filter = 
CALCULATETABLE(
    Orders, 
    Orders[Ship Mode]="Standard Class",
    FILTER(ALL('Date Master'),'Date Master'[Start Date] >_pre && 
            'Date Master'[Start Date] <= _cur))
var _summed = 
SUMMARIZE(
    _filter,
    [Customer ID],
    "sales",SUM([Sales]),
    "count",DISTINCTCOUNT([Order ID])                       
)
return  COUNTROWS(filter(_summed,[sales] >1000 && [count] <5))
I hope this helps. If it does, please Mark as a solution.
I also appreciate Kudos.
Nathan Peterson
sokg
Solution Supplier
Solution Supplier

Hi @Baskar 

 

Try to use SUMMARIZE  with addcolumns as the Italians said at the link below.

 

 

https://www.sqlbi.com/articles/best-practices-using-summarize-and-addcolumns/

Baskar
Resident Rockstar
Resident Rockstar

Thanks for your quick response my dear friend. 

 

There is no luck even tried with addcolumn. 

 

😞

 

It is taking 1.5 mins 

Helpful resources

Announcements
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors