DISCOUNT with multiple criteria in Power BI - if-statement

I have two tables are Data and Report.
Data Table: In the Data table, two columns are Item, Qty, and Order. The Item columns contain as a text & number and qty and number column stored as text and number.
The item column is repeated according to the order and the same item column contains two different qty according to the order column.
Report Table:
I have a unique item column.
Data and Report file looks like.
Data
ITEM QTY ORDER
123 200 1
123 210 0
5678 220 1
5678 230 0
5555 240 1
6666 250 1
9876 260 1
2345 270 1
901 280 1
901 280 1
902 300 1
902 300 1
123456 200 1
123456 200 1
123456 210 1
123456 210 1
123456 0 1
567 200 1
567 210 1
567 210 1
567 0 1
453 5000 1
453 5000 1
453 5000 1
453 5000 1
112 5000 1
112 5000 1
112 5000 1
112 5000 1
116 5000 1
116 5001 1
116 0 1
116 0 1
116 5000 0
116 5001 0
116 0 0
116 0 0
Report
ITEM DESIRED RESULT (QTY)
123 200
5678 220
5555 240
6666 250
9876 260
2345 270
901 280
902 300
123456 MIXED
567 MIXED
4444 NA
12 NA
10 NA
453 5000
112 5000
116 MIXED
Expand snippet
Desired Result
I would like to pull the qty against the order “1” from the data table into the report table according to the item.
If the item is found in the data table then return the qty in the report table according to the item. {Please refer to the “Data” and “Report table for item 123 and 5678 etc….}
If an item is not found in the data table then return “NA” in the report table according to the item. {Please refer to the “Data” and “Report table for item 10, 12,444}
The same item contains two different qty then returns as a text “Mixed” in the report table according to the item. {Please refer to the “Data” and “Report table for item 123456,116 & 567}
Currently I am using the following calculated column CURRENT DAX FOR QTY = LOOKUPVALUE(DATA[QTY],DATA[ITEM],'DESIRED RESULT'[ITEM],DATA[ORDER],1,"NA") enter image description here
It’s almost working fine but it’s giving the wrong result “NA” were two different qty for the same item & two different order (0,1) or (1) or (o) {Please refer to the “Data” and “Report table for item 123456, 116 & 567} but the desired result is “Mixed” those three items.
Note: I convert the qty column from number to text otherwise it gives an error, is there any alternative option to achieve my result.
Herewith attached the PBI file for your reference https://www.dropbox.com/s/hf40q27pvn3ij2g/DAX-LOOKUPVALUE%20FILTER%20BY.pbix?dl=0.

If I'm understanding correctly, this can be done with the method I suggested previously with the addition of a filter for DATA[ORDER] = 1.
IF (
CALCULATE ( DISTINCTCOUNT ( DATA[QTY] ), DATA[ORDER] = 1 ) > 1,
"MIXED",
CALCULATE ( SELECTEDVALUE ( DATA[QTY], "NA" ), DATA[ORDER] = 1 )
)

Related

How to compare values by different dates in PowerBI

My problem is related with PowerBI report
It is example table, Real table contains 10000+ results
user
salary
date
1
123
14-10-2022
2
455
11-10-2022
3
333
13-10-2022
4
222
12-10-2022
5
111
10-10-2022
desired output:
user
salary
date
salary (date-1 day)
salary (date-3 days)
1
123
14-10-2022
333
455
2
455
11-10-2022
111
3
333
13-10-2022
222
111
4
222
12-10-2022
455
5
111
10-10-2022
How can I achieve it in PBI ?
I tried to merge tables but the dashboard was very slow after try like that.
I would do this in DAX, not Power Query.
Add a date dimension (search on this if you aren’t familiar with date dimensions), then create these DAX measures.
Salary (date-1 day) = CALCULATE( SUM(TABLE[salary]), DATEADD(DateDim[DateKey],-1,day) )
Salary (date-3 day) = CALCULATE( SUM(TABLE[salary]), DATEADD(DateDim[DateKey],-3,day) )

How to concatenate two columns in oracle select query?

The following is the sample data of my table which I created it in Oracle 19c.
id
first_numbers
second_numbers
10
123
111
10
122
123
10
111
124
11
333
111
11
444
222
11
222
124
In above table some duplicate values are existed for each id, I want to remove the duplicate values for each id and I want the values of both columns to be shown in one column in the result query
The expected result which I want is:
id
concatenated_colums
10
123
10
111
10
122
10
124
11
333
11
111
11
222
11
124
Use a UNION statement:
SELECT id, first_numbers "concatenated_colums" FROM table
UNION
SELECT id, second_numbers "concatenated_colums" FROM table

How to select data from a table based on multiple expressions?

I have a view by the name of info and it's structure and data sample is the following:
id
name
contacts
1
ali
1234
1
ali
122
2
john
133
2
john
144
2
john
122
3
mike
111
4
khan
444
5
jan
122
5
jan
155
So I am using the above view data in oracle apex report. I want to search data by id for example I search for id=1, it contains two values in contacts column one of the value which is 122 is also included in another records so the result should also contain all the other records which contain 122 in their contacts column.
The expected result which I want is:
id
name
contacts
1
ali
1234
1
ali
122
2
john
133
2
john
144
2
john
122
5
jan
122
5
jan
155
We can phrase your requirement as wanting to return any record with id = 1 or any record whose contacts overlap with the contacts of id = 1.
SELECT id, name, contacts
FROM yourTable
WHERE id = 1 OR
id IN (
SELECT id
FROM yourTable
WHERE contacts IN (SELECT contacts FROM yourTable WHERE id = 1)
)
ORDER BY id;
Demo

Merging the two datasets

Dataset A:
Company_Name Match Sales EMPS
1234 0 0 0
1234 0 0 0
1234 0 0 0
5678 0 0 0
5678 0 0 0
5678 0 0 0
9123 9123 500 2
9123 9123 500 2
9123 9123 500 2
Dataset B:
Company_Name Match Sales EMPS
1234 1234 600 10
1234 1234 600 10
1234 1234 600 10
5678 5678 900 56
5678 5678 900 56
5678 5678 900 56
I am trying to merge the above tables using proc sql, and here is the desired output
Dataset A:
Company_Name Match Sales EMPS
1234 1234 600 10
1234 1234 600 10
1234 1234 600 10
5678 5678 900 56
5678 5678 900 56
5678 5678 900 56
9123 9123 500 2
9123 9123 500 2
9123 9123 500 2
However, when I try to do a join, it only takes the first table's values. I know I should do a case statement somewhere, but not sure how. For example, since datasetb had values for company_name=1234, the final output should capture that, and if there are no values, it should take the column values of the first table, if that makes sense
proc sql;
create table merge_table as
select a.*,b* from dataseta as a inner join datasetb as b on (a.company_name=b.company_name);quit;
Use the COALESCE() function to code your preference for B values over A value.
create table merge_table as
select a.company_name
, coalesce(b.match,a.match) as match
, coalesce(b.sales,a.sales) as sales
, coalesce(b.EMPS,a.EMPS) as EMPS
from dataseta as a
inner join datasetb as b
on (a.company_name=b.company_name)
;
But your example has repeats for COMPANY_NAME in both datasets. How do you want to handle that? Currently it will match each of the three records from A for company 1234 with each of the three records from B for company 1234 and produce 9 records for that company in the result set. You need some other variable(s) to include in the join condition so that a performs a 1 to 1 match (or at least a 1 to N match) instead of the current N to M match.
Assuming same number of zero rows and non-zero rows to replace, consider a union query to stack non-zeros and other dataset:
proc sql;
create table merge_table as
select b.Company_Name, b.Match, b.Sales, b.EMPS
from datasetb as b
union
select a.Company_Name, a.Match, a.Sales, a.EMPS
from dataseta as a
where (a.Match + a.Sales + a.EMPS) ^= 0;
quit;

Convert one table into another format from SAS code

I have a table is SAS which looks like this.
year Country Host Code Value
2010 India Pak 220 111
2010 India Aus 220 123
2010 India NZ 220 23
2010 India SA 240 43
2010 India WI 250 124
2010 India SRI 250 325
2010 India ZIM 280 235
i want to transform this table to following form
Country Code Pak_2010 Aus_2010 NZ_2010 SA_2010 WI_2010 SRI_2010 IM_2010
India 220 111 123 23 0 0 0 0
India 240 0 0 0 43 0 0 0
India 250 0 0 0 0 124 325 0
India 280 0 0 0 0 0 0 235
for one country and code, there will be one value.
Can anyone please suggest me code for doing this transformation?
This is a classical proc transpose, separating your ID variables with a delimiter:
PROC TRANSPOSE
DATA=yourInput
OUT=yourOutput(drop=_name_)
DELIMITER=_;
BY Country Code;
ID Host Year;
VAR Value;