Forum Discussion
DAX Help - counting with a 'contains'
Hi there,
I have two tables, a Sales table and a User table something like this:
Sales
| Customer No | Order No | Product Code | Order No & Product Code | Qty |
| 1 | 111 | 123 | 111-123 | 2 |
| 1 | 111 | 456 | 111-456 | 1 |
| 1 | 111 | 789 | 111-789 | 5 |
| 2 | 222 | 123 | 222-123 | 3 |
| 2 | 222 | 456 | 222-456 | 2 |
| 3 | 333 | 123 | 333-123 | 6 |
| 4 | 444 | 123 | 444-123 | 7 |
| 4 | 444 | 789 | 444-789 | 1 |
| 5 | 555 | 456 | 555-456 | 1 |
| 5 | 555 | 789 | 555-789 | 2 |
Users
| Customer No | Username | Products list | Product 123 access | Product 456 access | Product 789 access |
| 1 | Jon | 111-123 | 111-456 | Yes | Yes | No |
| 1 | Jill | 111-456 | 111-789 | No | Yes | Yes |
| 2 | Bill | 222-123 | 222-456 | Yes | Yes | No |
| 2 | Bob | 222-123 | 222-456 | Yes | Yes | No |
| 2 | Susan | 222-123 | 222-456 | Yes | Yes | No |
| 3 | Sarah | 333-123 | Yes | No | No |
| 4 | Lisa | 444-123 | Yes | Yes | No |
| 4 | Alan | 444-123 | 444-789 | Yes | Yes | Yes |
| 4 | Jilly | 444-789 | No | No | Yes |
| 5 | Jacob | 555-456 | 555-789 | No | Yes | No |
| 6 | William | 555-456 | 555-789 | No | Yes | Yes |
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 No | Order No | Product Code | Qty purchased | Qty allocated |
| 1 | 111 | 123 | 2 | 1 |
| 1 | 111 | 456 | 1 | 2 |
| 1 | 111 | 789 | 5 | 1 |
| 2 | 222 | 123 | 3 | 3 |
| 2 | 222 | 456 | 2 | 3 |
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
Community 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
- Ashish_Mathur
Super User