Forum Discussion
Help witch fetching values from another table depending on conditions
Hi
I’m new do DAX and PowerBI and trying to get the hang of it, but I’m stuck. I will try to the best of my abilities describe what I need to do. I would like to create a new column in my datatable called “Category”, in which a text is added depending on conditions in the form of a “Code”. If no condition is found in my rules table, fetch the “DefaultCategory” from my suppliers table.
I have my Datatable as example below:
SupplierID | SupplierName | Amount | Code | Category |
123 | Name 1 | 1000 | 55 |
|
123 | Name 1 | 500 | 33 |
|
555 | Name 2 | 750 | 55 |
|
555 | Name 2 | 750 | 44 |
|
I also have a table for my suppliers:
SupplierID | SupplierName | DefaultCategory |
123 | Name 1 | Categoryname 1 |
555 | Name 2 | Categoryname 1 |
I also have a table for rules to override the DefaultCategory:
SupplierID | Code | NewCategory |
123 | 33 | Categoryname 2 |
555 | 44 | Categoryname 3 |
I would like the result to return like this:
ID | SupplierName | Amount | Code | Category |
123 | Name 1 | 1000 | 55 | Categoryname 1 |
123 | Name 1 | 500 | 33 | Categoryname 2 |
555 | Name 2 | 750 | 55 | Categoryname 1 |
555 | Name 2 | 750 | 44 | Categoryname 3 |
If anyone could help me I would really appreciate it.
I add an extra search value in the LOOKUPVALE() to match the first criteria. In the first line were checking the rule code and supplier, if they match with Data[Code]. We return the NewCategory. Otherwise the DefaultCategory.
Example:IF( LOOKUPVALUE(Rules[Code], Rules[SupplierIDCode], Data[SupplierIDCode], Rules[SupplierID], Data[SupplierID]) = Data[Code] , LOOKUPVALUE(Rules[NewCategory],Rules[Code],Data[Code]) & "From Rules" , LOOKUPVALUE(Suppliers[DefaultCategory],Suppliers[SupplierID],Data[SupplierID]) )
10 Replies
- SomeDataDudeAdvocate I
- MyshyddeFrequent Visitor
Yes, this worked! Thank you for your swift answer!
- MyshyddeFrequent Visitor
I've got myself in some trouble with this solution. While it works for single rows in the "Rules table", if there are multiple rules for the same supplier (other codes) then there is an error. I suppose I need another dimension of filters in the lookup?
- SomeDataDudeAdvocate I