Merging two tables in Powerquery using both a case clause and a where function (should I use something else?

69 Views Asked by At

I am trying to merge two tables in Powerquery. One is from a database, the other is from another source and has a set list of part numbers (pano20). Each part number has a source of supply (sos1) and some part numbers have two or more sources of supply each with differing unit costs (uncs). For example, part number 123 has a source of supply of both 000 and 530, but its unit cost under 000 is $1.50 and under 530 it is $2.50. There are 3 part numbers in my non-database table that I need to come from sos1 530 and the rest need to come from sos1 000. I am unsure which SQL to use, but I have used the one below. Obviously it is not working but I wanted to see if anyone had an idea of how I could do this.

use LIBCOM500

select sos1, uncs, ds18,
       case pano20
       when pano20 in ('ca2','oa1','oa1p') then sos1 = '530'
       else sos1 = '000'
from pcpprms0

Below is the desired output after merging the two tables.

Table 1 - excel table
|Part Number|Qty Needed|
|:---------:|---------:|
|    123    |     1    |
|    222    |     3    |

Table 2 - coming from database
|Part Number|SOS1|uncs|
|:---------:|:--:|---:|
|    123    |000 |1.50|
|    123    |530 |2.50|    
|    222    |000 |3.00|

Table 3 - Output from merging table 1 and 2 by part number
|Part Number|SOS1|uncs|Qty Needed|
|:---------:|:--:|:--:|---------:|
|    123    |530 |2.50|    1     |
|    222    |000 |3.00|    3     |

2

There are 2 best solutions below

9
Isolated On

This would be the proper and common format of a case expression. You have too much in the expression and you didn't END it.

select sos1, uncs, ds18,
case 
 when pano20 in ('ca2','oa1','oa1p') then '530'
 else '000'
end sos1b
from pcpprms0
4
sanjay bhansali On

Try this query may be it will help

select case when pano20 in ('ca2','oa1','oa1p') then '530' else '000'
end as sos1,uncs, ds18 from pcpprms0