I am trying to create an ID based in multiple values in different columns in Power query.
The idea is to check the following values:
IF
ID_STORE = 1
ID_PRODUCT = 1
ID_CATEGORY = 1
SALE_DATE = 01/01/2018
ID_COSTUMER = 1
THEN CREATE THE SAME ID FOR THE ROWS THAT HAVE THIS INFO.
The idea is to check the rows that have that info (1 and 01/01/2018) in multiple columns (ID_STORE, ID_PRODUCT, ID_CATEGORY etc..).
Thanks in advance.
Obs: This is my first post, so feel free to correct me in any manner.
You need to add an 'AND' clause to your 'OF' statement;
if [ID_STORE] = 1
and [ID_PRODUCT] = 1
and [ID_CATEGORY] = 1
and [SALE_DATE] = Text.ToDate("01/01/2018")
and [ID_COSTUMER] = 1 then
1 else 0
Related
I am using this:
'Total = SUMX(
FILTER(
Table1,
Table1[Client] = EARLIER( Table1[Client] )
),
Table1[Sales])'
In the table I have many results that are duplicated in the column created here, I still have a use for the value but I want a way to possibly insert 'nulls' or blanks in instances where 'Client/Sales' pairs (across the rows) are already accounted for. Is there a way to take both columns and when an instance is 'repeated' to insert a blank?
This is what things look like now...
Client
Sales
Total
1hk4
4
8
1hk4
2
8
4f33
1
3
5t23
3
5
4f33
1
3
1hk4
2
8
4f33
1
3
5t23
2
5
This is what I'm trying to go for:
Client
Sales
Total
1hk4
4
8
1hk4
2
4f33
1
3
5t23
3
5
4f33
1
1hk4
2
4f33
1
5t23
2
The reason I'm trying for this is because in the report I am unable to turn off summarize and the data doesn't relay well. Ideally I want to be able to see the unique result by client.
Lets say you can add a column to your table with a unique index for each row:
Then you can add a calculated column like this:
Total =
var currnetIndex = [Index]
var currentClient = [Client]
var minIndex =
CALCULATE(
MIN('Data'[Index]),
FILTER(
ALL('Data'),
'Data'[Client] = currentClient
)
)
return
IF(
currnetIndex = minIndex,
CALCULATE(
SUM('Data'[Sales]),
FILTER(
ALL(Data),
'Data'[Client] = currentClient
)
),
BLANK()
)
Which will give you a table looking like this:
This solution works if the index is unique only to the user as well.
we have a scenario where as per group by key columns i have to get only record having combination of 0 & any valid number for column "ID"
with the below example
TransId,CustomerNm,Date ,Number,Gender,ID
1 ,Surya ,2020-01-01,123456,M ,1234
1 ,Surya ,2020-01-01,123456,M ,0
2 ,Naren ,2020-01-20,123456,M ,3456
2 ,Naren ,2020-01-20,123456,M ,6789
When i try with the below query (key columns: TransId,CustomerNm,Date,Number,Gender)
select TransId,CustomerNm,Date,Number,Gender from INS.Transaction
group by TransId,CustomerNm,Date,Number,Gender having count(*) > 1
i will get both the records
1 ,Surya ,2020-01-01,123456,M
2 ,Naren ,2020-01-20,123456,M
but i am trying to get the records having ID =0 . Expecting output as
1 ,Surya ,2020-01-01,123456,M
Pls suggest if i can add any change in the above query
while doing group by add MIN(ID) in select query and then fetch id 0
select * from (
select TransId,CustomerNm,Date,Number,Gender,MIN(ID) ID from INS.Transaction
group by TransId,CustomerNm,Date,Number,Gender having count(*) > 1 ) where ID = 0
Just add another AND to the having clause to check if min(id) = 0 . I have renamed some columns.
select transid,customernm,transaction_date,some_number,gender
from transaction
group by transid,customernm,transaction_date,some_number,gender
having count(*) > 1 and min(id) = 0;
Note:- Do not use reserved words as column name like date and number
I have one table scores where I have saving users scores. It's looks like this
table `scores`
id | points | user_id
1 5 1
2 2 1
3 4 1
4 1 3
5 10 2
I want to select each user, sum his points and show as a ranking. The result from above should be
user_id | points
1 11
2 10
3 1
The query with which I came up is
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->first();
The problem is in ->first() because it's return only one result.. it is working as must. If I try to use ->get() instead I've got Undefined property error. How should I use this?
The query which is working in phpmyadmin
SELECT count(id) as numberId, sum(points) as numberOfPoints FROM `points` GROUP BY `user_id`
You can use something like this
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->get();
foreach($sumPoints as $point){
dd($point); //OR dd($point->numberOfPoints)
}
I want to sort a record in following way.
Arrange records in group (by ID column)
Sort the step 1 results by ascending order (by NAME column)
2.1. If NAME Column having same values, then order by FLAG column value (ascending order)
Order the Step 2 results by Order Assist column (I will be passing dynamic value to sort using order assist column)
My Query:
SELECT IDENTIFIER, CODE, INC_EXC_FLAG,ORDER_ASSIST FROM DUMMY_SORT
WHERE METHOD_ID = '1'
GROUP BY (IDENTIFIER, CODE, INC_EXC_FLAG,ORDER_ASSIST)
ORDER BY ORDER_ASSIST ASC, CODE ASC, INC_EXC_FLAG ASC
Result of above Query:
ID NAME FLAG ORDER_ASSIST
A_EC AEC 0 EC1
B_EC_DET BEC 1 EC2
A_NIT ANIT 0 NIT1
A_NIT ANIT 1 NIT1
A_NIT BNIT 0 NIT1
B_NIT_DET BNIT 0 NIT2
B_NIT_DET BNIT 1 NIT2
A_SC ASC 0 SC1
A_SC ASC 1 SC1
B_SC_DET BSC 0 SC2
B_SC_DET BSC 1 SC2
C_SC_FUN CSC 0 SC3
D_SC_GRP DSC 0 SC4
But I want to generate the result according to dynamic values of order_assist
For Example:
If I am passing dynamic value as "SC" i want to fisrt order the records SC1,SC2,SC3. Then NIT1,NIT2 . then EC1,EC2.
If I am passing dynamic value as "NITG" i want to fisrt order the records NIT1,NIT2 then SC1,SC2,SC3. then EC1,EC2.
Expected result added when dynamic value is "SC"
ID NAME FLAG ORDER_ ASSIST
A_SC ASC 0 SC1
A_SC ASC 1 SC1
B_SC_DET BSC 0 SC2
B_SC_DET BSC 1 SC2
C_SC_FUN CSC 0 SC3
D_SC_GRP DSC 0 SC4
A_NIT ANIT 0 NIT1
A_NIT ANIT 1 NIT1
A_NIT BNIT 0 NIT1
B_NIT_DET BNIT 0 NIT2
B_NIT_DET BNIT 1 NIT2
A_EC AEC 0 EC1
B_EC_DET BEC 1 EC2
Sounds like maybe you're after something like:
order by case when p_sort_param = 'SC' and order_assist like 'SC%' then 1
when p_sort_param = 'SC' and order_assist like 'NIT%' then 2
when p_sort_param = 'NITG' and order_assist like 'NIT%' then 1
when p_sort_param = 'NITG' and order_assist like 'SC%' then 2
else 3
end,
order_assist
where p_sort_param is the parameter that gets passed in to provide the "dynamic" value. This assumes you're running the query via a stored procedure. If it's a manually run query (eg. in Toad), then add a colon in front of the parameter name to make :p_sort_param.
I cannot understand your specific ordering rules, but you should be able to achieve what you want using CASE expressions:
order by
case order_assist
when 'SC' then <first thing to order by for SC>
when 'NITG' then <first thing to order by for NITG>
...
end,
case order_assist
when 'SC' then <second thing to order by for SC>
when 'NITG' then <second thing to order by for NITG>
...
end,
... etc.
i have problem with this case, i have log table that has many same ID with diferent condition. i want to select two max condition from this. i've tried but it just show one record only, not every record in table.
Here's my records table:
order_id seq status____________________
1256 2 4
1256 1 2
1257 0 2
1257 3 1
Here my code:
WITH t AS(
SELECT x.order_id
,MAX(y.seq) AS seq2
,MAX(y.extern_order_status) AS status
FROM t_order_demand x
JOIN t_order_log y
ON x.order_id = y.order_id
where x.order_id like '%12%'
GROUP BY x.order_id)
SELECT *
FROM t
WHERE (t.seq2 || t.status) IN (SELECT MAX(tt.seq2 || tt.status) FROM t tt);
this query works, but sometime it gave wrong value or just show some records, not every records.
i want the result is like this:
order_id seq2 status____________________
1256 2 4
1257 3 2
I think you just want an aggregation:
select d.order_id, max(l.seq2) as seq2, max(l.status) as status
from t_order_demand d join
t_order_log l
on d.order_id = l.order_id
where d.order_id like '%12%'
group by d.order_id;
I'm not sure what your final where clause is supposed to do, but it appears to do unnecessary filtering, compared to what you want.