Concatenate X not sorting correctly

Hi guys

I’m preparing a measure to concatenate items selected from a slicer. Where there are more than 5 items selected, the measure should read the first 5 with “and more” afterwards.

The problem is that I cannot get them to sort in the same order as the slicer is showing. They just appear randomly, even with Order By expressions.

[MA Report] is sorted by [MARepId] in the model already, so I would have expect it just to work. Reading up on this TopN doesn’t guarantee sort order. Grrrr.

So how do I overcome? NB the production model has 156 MA Reports, not just the 11 in the uploaded PBIX.

Thanks
Pete
RepPage.pbix (35.8 KB)

@BINavPete

MAReport Selection = 
VAR __DISTINCT_VALUES_COUNT =
    DISTINCTCOUNT ( MAReports[MA Report] )
VAR __MAX_VALUES_TO_SHOW = 5
VAR _LF =
    UNICHAR ( 10 )
VAR topx =
    TOPN (
        __MAX_VALUES_TO_SHOW,
        ALLSELECTED ( MAReports[MA Report], MAReports[MARepId] ),
        MAReports[MARepId], ASC
    )
VAR Result =
    IF (
        __DISTINCT_VALUES_COUNT > __MAX_VALUES_TO_SHOW,
        CONCATENATE ( CONCATENATEX ( topx, MAReports[MA Report], _LF ), ", and more." ),
        CONCATENATEX (
            SUMMARIZE ( MAReports, MAReports[MA Report], MAReports[MARepId] ),
            MAReports[MA Report],
            _LF,
            MAReports[MARepId], ASC
        )
    )
RETURN
    Result
2 Likes

Thanks @AntrikshSharma.

@BrianJ said you were the master. :grinning:

I did try something along the lines of your solution but got the red underline from intellisense so left it. Learning for me is not to rely on intellisense.

Thanks again
Pete

@BINavPete Yeah, the new version of Power BI has a bug for ALLSELECTED, not sure why they haven’t fixed it yet. I am using August 2021 release lol.

Hi @AntrikshSharma

When I select more than 5, It’s not showing in the same order.
I just copied your measure and created card visual. Am I missing anything here ?

@Rajesh Yeah, the result of TOPN isn’t guaranteed to be sorted so need to use ORDER BY argument in CONCATENATEX and can skip the ordering in TOPN

CONCATENATE ( CONCATENATEX ( topx, MAReports[MA Report], _LF, MAReports[MARepId] ), “, and more.” )

Thanks @AntrikshSharma

@BINavPete It’s interesting. I tried, PFB my approach

I created an Index column using DAX, If you want you can create in PQ also and a measure as below

RepPage.pbix (36.8 KB)

@Rajesh
I learnt a whole heap from this once I’d looked up what the Unicode. So thanks ever so much. The techniques will be useful elsewhere.

Unfortunately it still doesn’t quite work. I attach PBIX with full list of possible reports as it appears in the production model. The mockup deduplicated the records and filtered out a load, just to make the problem easier to solve.

I have overcome the multiple record thing easily, by inserting a virtual table (vJustReports) to get unique MA Report and index.

But in example showing parts before expenses? It may be that I need to resequence it all but looking at the MAReports table I can;t see a reason why your solution won’t work
RepPage.pbix (42.5 KB)
Pete

@BINavPete

I did small change to your measure. Now it’s showing in the same order.

RepPage.pbix (41.9 KB)

@BINavPete

No need to create index column, create a measure as below.

Apologies for late reply @Rajesh .
Been busy on an anonymisation project.

Just perfect. Thanks for help
Pete