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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
barragan82
Advocate I
Advocate I

Measure: Special Instructions for CountItems = 2

Hi Everyone,

 

I have a measure that auto populates items. It provides a comma between each item, and includes the word "and" before the last item. I would like it so that when there are only two items it does not include a comma but does include the word "and." 

 

Example: 

Currently the measure displays like this: Item 1, and Item 2

I would like it to display like this: Item 1 and Item 2

 

I would like it to continue adding a comma and the word "and" when there are three or more items (ex. Item 1, Item 2, and Item 3). Below is a screenshot of the measure I am using. Thank you all for your help!

 

barragan82_0-1758642966281.png

 

 

1 ACCEPTED SOLUTION
mh2587
Super User
Super User

Measure = //Try this might help you
VAR ItemsList =
    FILTER (
        DISTINCT ( 'Master Core AY24-25'[WC Assignment Type] ),
        LEN ( TRIM ( COALESCE ( 'Master Core AY24-25'[WC Assignment Type], "" ) ) ) > 0
    )
VAR CountItems = COUNTROWS ( ItemsList )
VAR ConcatText =
    CONCATENATEX (
        ItemsList,
        TRIM ( COALESCE ( 'Master Core AY24-25'[WC Assignment Type], "" ) ),
        ", ",
        TRIM ( COALESCE ( 'Master Core AY24-25'[WC Assignment Type], "" ) ),
        ASC
    )
VAR LastItem =
    MAXX ( ItemsList, TRIM ( COALESCE ( 'Master Core AY24-25'[WC Assignment Type], "" ) ) )
RETURN
SWITCH (
    TRUE (),
    CountItems = 0, BLANK(),
    CountItems = 1, LastItem,
    CountItems = 2,
        -- For exactly two items, replace comma with " and "
        SUBSTITUTE ( ConcatText, ", ", " and " ),
    SUBSTITUTE ( ConcatText, ", " & LastItem, " and " & LastItem )
)

Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



View solution in original post

3 REPLIES 3
mh2587
Super User
Super User

Measure = //Try this might help you
VAR ItemsList =
    FILTER (
        DISTINCT ( 'Master Core AY24-25'[WC Assignment Type] ),
        LEN ( TRIM ( COALESCE ( 'Master Core AY24-25'[WC Assignment Type], "" ) ) ) > 0
    )
VAR CountItems = COUNTROWS ( ItemsList )
VAR ConcatText =
    CONCATENATEX (
        ItemsList,
        TRIM ( COALESCE ( 'Master Core AY24-25'[WC Assignment Type], "" ) ),
        ", ",
        TRIM ( COALESCE ( 'Master Core AY24-25'[WC Assignment Type], "" ) ),
        ASC
    )
VAR LastItem =
    MAXX ( ItemsList, TRIM ( COALESCE ( 'Master Core AY24-25'[WC Assignment Type], "" ) ) )
RETURN
SWITCH (
    TRUE (),
    CountItems = 0, BLANK(),
    CountItems = 1, LastItem,
    CountItems = 2,
        -- For exactly two items, replace comma with " and "
        SUBSTITUTE ( ConcatText, ", ", " and " ),
    SUBSTITUTE ( ConcatText, ", " & LastItem, " and " & LastItem )
)

Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



No need to reply to my previous question. I was able to get the result I was looking for by making the changes indicated in red. Just posting as FYI for the forum. Thanks!

barragan82_0-1758655241181.png

 

Thanks for your help, @mh2587! Unfortantely, some of my items have commas in them and are being converted to "and." For example, my Item 1 is "oranges, lemons, limes, etc.", but it is now being displayed as "oranges and lemons and limes and etc." Do you have any other suggestions?

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.