Forum Discussion

RichardP's avatar
RichardP
Icon for Helper I rankHelper I
7 years ago
Solved

DAX Help - counting with a 'contains'

Hi there,

 

I have two tables, a Sales table and a User table something like this:

 

Sales

Customer NoOrder NoProduct CodeOrder No & Product CodeQty
1111123

111-123

2
1111456111-4561
1111789111-7895
2222123222-1233
2222456222-4562
3333123333-1236
4444123444-1237
4444789444-7891
5555456555-4561
5555789555-7892

 

Users

Customer NoUsernameProducts listProduct 123 accessProduct 456 accessProduct 789 access
1Jon111-123 | 111-456YesYesNo
1Jill111-456 | 111-789NoYesYes
2Bill222-123 | 222-456YesYesNo
2Bob222-123 | 222-456YesYesNo
2Susan222-123 | 222-456YesYesNo
3Sarah333-123YesNoNo
4Lisa444-123YesYesNo
4Alan444-123 | 444-789YesYesYes
4Jilly444-789NoNoYes
5Jacob555-456 | 555-789NoYesNo
6William555-456 | 555-789NoYesYes

 

Currently there is a linking table of unique Customer Numbers that joins the two tables.

 

What I'm trying to do is identify discrepancies between what sales are recorded in the Sales table and what the allocation of licenses looks like in the Users table.

 

The desired output would look something like this:

Customer NoOrder NoProduct CodeQty purchasedQty allocated
111112321
111145612
111178951
222212333
222245623

 

The "Qty allocated" comes from counting the number of "Yes" values in the column relevant to the Product Code where the User table's Products List contains the same text as the Sales table's Order No & Product Code.

 

There are only 8 different Product Codes so I was considering a nested IF to calculate each one.

 

The closest I've managed to get for Product 123 was this:

 

IF(CONTAINSSTRING('Sales'[Order No & Product Code],"123"),CALCULATE(COUNTROWS('Users'),FILTER('Users',[Product 123 access]="Yes" && (CONTAINSSTRING('Users'[Products list],"123")))),0)

 

However I get the message: "A single value for column 'Order No & Product Code' in table 'Sales' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result."

 

As always, any help from the community is gratefully received.

Richard

2 Replies

  • HotChilli's avatar
    HotChilli
    Icon for Community Champion rankCommunity Champion

    Instead of parsing the column and writing separate measures per product, I suggest you re-shape your User table in Power Query editor so that you have  

     

    Customer User  Product

     

    You could do this by duplicating the User Query, drop the last 3 columns from both.  Split the Products List column into two,  based on the | separator.

    Append the two user queries (you'll have to remove columns and tidy the data).

    You can then drop your linking table and instead join the User to Customer on 'Order No Product Code' and 'Product'.

     

    Once you've done that, you can pull Qty from the Customer table and pull COUNT of Product from the Users table.

     

    Let me know how you get on