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
Rick_Gregory
Frequent Visitor

DAX Trickery

The problem:

     Simple, I need to bucket customer counts based on their growth % from the previous month (ie How many customers grew their sales 10-15%,16-20%, etc).

     In my fact table, there are 3 columns: Date, Total Sales, & Customer ID. I do have a date table.

 

What I've been trying to do is create a disconnected table that shows Date,  Customer ID,Sales, & Previous Months Sales (using a calculated column) so I could then use the growth percentage on an X-axis using a customer count of as my values.

 

Now, I know I can use a PreviousMonth measure to achieve this, but I belive I need this in the form of a column in this case so I can use it on an axis.

 

Thoughts?

1 ACCEPTED SOLUTION

@Rick_Gregory

Here's an example of the Dynamic Segmentation approach I was thinking of.

 

Here's a sample PBIX.

 

I used a pattern similar to one presented here:

https://www.sqlbi.com/articles/optimizing-duplicated-dax-expressions-using-variables/

 

 

Data model:image.png

 

 

Then the Segmentation measures are:

 

Customers by Segment = 
SUMX (
    Segments,
    COUNTROWS (
        FILTER (
            VALUES ( Sales[Customer ID] ),
            VAR MoMPct = [Sales MoM %]
            RETURN
                NOT ( ISBLANK ( MoMPct ) )
                && MoMPct >= Segments[Lower]
                && MoMPct < Segments[Upper]
        )
    )
)
Sales by Segment = 
SUMX (
    Segments,
     SUMX ( 
         VALUES ( Sales[Customer ID] ),
         VAR MoMPct = [Sales MoM %]
         VAR SalesMeasure = [Sales]
         RETURN
             IF (
                 NOT ( ISBLANK ( MoMPct ) )
                 && MoMPct >= Segments[Lower]
                 && MoMPct < Segments[Upper],
                 SalesMeasure
             )
    )
)

Both measures assume Segments don't overlap, otherwise they would have to be written more like the versions on DAX Patterns.

 

Output of measures looks like:image.png

 

 

Regards,

Owen

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

6 REPLIES 6
OwenAuger
Super User
Super User

Hi @Rick_Gregory

After reading your question, I think this is a perfect case for dynamic segmentation!

https://www.daxpatterns.com/dynamic-segmentation/

 

This would involve a disconnected table specifying the % ranges, then creating a segmentation measure using the pattern at the above link.

 

I don't have time to work up an example right now but suggest you give it a go & post back if needed.

 

Generally I prefer a dynamic approach with measures that will respond to filter context, rather than a calculated table that may solve one instance of a problem. For example, later on you might want to segment customers based on growth in a certain type of sales or in certain regions etc.

 

Best regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

That was my original thought as well, but I've had no luck with that in this case.

@Rick_Gregory

Here's an example of the Dynamic Segmentation approach I was thinking of.

 

Here's a sample PBIX.

 

I used a pattern similar to one presented here:

https://www.sqlbi.com/articles/optimizing-duplicated-dax-expressions-using-variables/

 

 

Data model:image.png

 

 

Then the Segmentation measures are:

 

Customers by Segment = 
SUMX (
    Segments,
    COUNTROWS (
        FILTER (
            VALUES ( Sales[Customer ID] ),
            VAR MoMPct = [Sales MoM %]
            RETURN
                NOT ( ISBLANK ( MoMPct ) )
                && MoMPct >= Segments[Lower]
                && MoMPct < Segments[Upper]
        )
    )
)
Sales by Segment = 
SUMX (
    Segments,
     SUMX ( 
         VALUES ( Sales[Customer ID] ),
         VAR MoMPct = [Sales MoM %]
         VAR SalesMeasure = [Sales]
         RETURN
             IF (
                 NOT ( ISBLANK ( MoMPct ) )
                 && MoMPct >= Segments[Lower]
                 && MoMPct < Segments[Upper],
                 SalesMeasure
             )
    )
)

Both measures assume Segments don't overlap, otherwise they would have to be written more like the versions on DAX Patterns.

 

Output of measures looks like:image.png

 

 

Regards,

Owen

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Ah, that worked! Absolutely brilliant, thank you!

v-chuncz-msft
Community Support
Community Support

@Rick_Gregory,

 

Yes, you may add a calculated table that contains the categories.

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.
Rick_Gregory
Frequent Visitor

^

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.