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
misul
Helper I
Helper I

Dynamic Segmentation - with duplicate rows

Hi, 

I am trying to follow this blog post about Dynamic Segmentation. https://www.daxpatterns.com/dynamic-segmentation/

 

I am stuck on one point - what to do when there are duplicate transactions? 

 

Dynamic segmentation1.PNGDynamic segmentation2.PNG

 

As you can see in the image, the category applied to Product A is not correct. 

 

Any help would be much appreciated. Thank you!

 

1 ACCEPTED SOLUTION

@misul

 

I have used this MEASURE in the LookUp Table

 

Measure =
VAR mysales =
    CALCULATE ( SUM ( Table1[Sales] ) )
RETURN
    CALCULATE (
        SUM ( Table1[Sales] ),
        FILTER (
            VALUES ( Table1[Product] ),
            mysales >= SELECTEDVALUE ( CategoryLookup[Lower] )
                && mysales < SELECTEDVALUE ( categoryLookup[Upper] )
        )
    )

 

dynamicsegment.png


Regards
Zubair

Please try my custom visuals

View solution in original post

19 REPLIES 19
Zubair_Muhammad
Community Champion
Community Champion

HI @misul

 

Please Try this revision. Just summing the sales for product before determining category

 

Applied Category =
VAR Mysales =
    CALCULATE ( SUM ( Table1[Sales] ), ALLEXCEPT ( Table1, Table1[Product] ) )
RETURN
    CALCULATE (
        VALUES ( CategoryLookup[Description] ),
        FILTER (
            CategoryLookUp,
            MySales >= CategoryLookup[Lower]
                && Mysales < categoryLookup[Upper]
        )
    )

 

 


Regards
Zubair

Please try my custom visuals

Many thanks for the quick reply @Zubair_Muhammad. Now I can slice by the AppliedCategory and so it solves my first issue.

 

The next step for me is to "dynamically change" the applied category when any other slicer is applied (such as Year or Region).  

 

As you can see in the image below, when I select the year 2016, the applied categories are not correct anymore. I am trying to follow the link from DAX patterns but I am not sure I am going down the right road. 

 

 

 Dynamic Segmentation3.PNG

 

 

Hi @misul

 

Then you will need to use a MEASURE....becasue calculated columns are not dynamic

 

I have not tested it but try this MEASURE in the CategoryLookup Table

 

Measure =
CALCULATE (
    SUM ( Table1[Sales] ),
    FILTER (
        Table1,
        Table1[Sales] >= CategoryLookup[Lower]
            && Table1[Sales] < categoryLookup[Upper]
    )
)

Regards
Zubair

Please try my custom visuals

@Zubair_Muhammad You are right, I would need to use a measure... I tested the formula and gives the following error. Dynamic segmentation4.PNG

 

But I have two follow-up questions : a) what is the idea behind creating a measure in the Lookup table instead of my Fact table?

 

b) Would it be possible to modify the initial formula for a calculated column "AppliedCategory" by asking it to sum up the sales values for a given year or region?

 

 

 

 

Sorry I forgot it's a measure
Here is the revised measure


Measure =
CALCULATE (
SUM ( Table1[Sales] ),
FILTER (
Table1,
Table1[Sales] >= selectedvalue(CategoryLookup[Lower])
&& Table1[Sales] < selectedvalue(categoryLookup[Upper])
)
)

Regards
Zubair

Please try my custom visuals

I will get back to you on your questions

I am out of office now.. On my mobile

You can share your file with me as well if possible for you

Regards
Zubair

Please try my custom visuals

@Zubair_Muhammad

Much appreciated. Can't upload my source files, but I have posted sample fact and lookup tables in images above.

 

Would be great to have any info on how to achieve this dynamic slicing .. with or without using the following methods.

 

Topics I have researched:

a. Dynamic documentation ( https://www.daxpatterns.com/dynamic-segmentation/) - My desired solution seems to match this, but there are no duplicate rows in their sample file, so I couldn't follow this.

b. SUMMARISE ROLLGROUP function -https://curbal.com/blog/glossary/summarize-dax  ( still figuring out how to use this function) 

EDIT: It seems that what I need is the ABC classification. Can anybody help to translate this to my simple table please?

 

https://www.daxpatterns.com/abc-classification-dynamic/

@Zubair_Muhammad Just wondering if you could help me out with my  first question.. 

 

a) what is the idea behind creating a measure in the Lookup table instead of my Fact table? How can this apply the dynamic category when slicing per year and per region?

 

HI @misul

 

Sorry for late reply.

 

Actually this way MEASURE will return values/sales amount which can be shown in VALUES section of TABLE or MATRIX visualization

 

If you create a MEASURE to compute categories in the fact TABLE..you cannot put them in TABLE rows

 

If you could copy paste some sample data from Excel.... I will try to work it out for you by creating a small pbix file

 


Regards
Zubair

Please try my custom visuals

@Zubair_Muhammad I also found that link, that's how I knew my problem is called "dynamic segmentation". I will try to follow the link again, but couldn't translate it to my solution.

 

Here are my tables. 

 

Table 1

YearProductSalesRegion
2016A5North
2016A5North
2016A10South
2017A10East
2016B3East
2017B2000South
2016C5West
2016C5North
2016D23West
2017D1South
2016E2North
2016E1South
2017E50West
2017F13East

 

CategoryLookup Table

 

LowerUpperDescriptionSortOrder
014<151
159915-1002
100499100-5003
5009999>5004

HI @misul

 

Please see the attached file here

With your sample data


Regards
Zubair

Please try my custom visuals

@misul

 

I have used this MEASURE in the LookUp Table

 

Measure =
VAR mysales =
    CALCULATE ( SUM ( Table1[Sales] ) )
RETURN
    CALCULATE (
        SUM ( Table1[Sales] ),
        FILTER (
            VALUES ( Table1[Product] ),
            mysales >= SELECTEDVALUE ( CategoryLookup[Lower] )
                && mysales < SELECTEDVALUE ( categoryLookup[Upper] )
        )
    )

 

dynamicsegment.png


Regards
Zubair

Please try my custom visuals

@Zubair_Muhammad Many thanks!!!!!! This seems to work.

 

I will apply it to my data (which has more rows and more granularity) ... and hopefully it will still work.

 

 

 

@Zubair_Muhammad I celebrated too early. The current setup doesn't allow me to count the number of rows in each category, which is my final goal.

 

for example: How many products with Sales <15 in 2017 in Region North? 

Capture3.PNG

Hi @misul

 

To count the products,,,,you can try this MEASURE

 

CountProducts =
CALCULATE (
    DISTINCTCOUNT ( Table1[Product] ),
    FILTER ( ALL ( Table1[Product] ), [Measure] > 0 )
)

Regards
Zubair

Please try my custom visuals

It solves my issue. Thanks a million! @Zubair_Muhammad

 

 

For reference, this post shows how to:

a. Categorise your data into buckets of 0-15, 15-100 etc

b. Distinct count those categories

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.