How to filter Table in DAX:
Example (.pbix):
How to return only "highlighted yellow" rows (rows with max dy_UpCst per ki_File)?
P.S. I know how to do it with SQL or PowerQuery. In two steps: 1) get table with ki_File and max dy_UpCst, 2) join all necessary values from source table
But I don't know how to do it with DAX.
And, as far as I know, DAX could contain very elegant solutions.
You can use the following formula to generate a ranking column, assuming that in your case ID column is ki_File and DATE column is dy_UpCst. In this way if you filter by the ranking column, selecting value=1 you will highlight the rows you desire.
Rank =
VAR x = 'Table'[ki_File]
VAR y = 'Table'[dy_UpCst]
RETURN
CALCULATE (
RANK.EQ ( y, 'Table'[dy_UpCst], DESC ),
FILTER ( ALL ( 'Table' ), 'Table'[ki_File] = x )
)
Let me know if this works for you!
Related
Oracle 19.3 on Win2019
Looking for a solution to pivot data based on the subquery. I looked for examples, but all I found is the "pivot xml" solution, which returns xml-formated column.
Here is my query:
with assign_data as
(
select --*
style_id
,color_id
,size_id
,in_whse_date
,sum(pd2_assign_so_quantity) qty
from pd2_assignment
where business_unit_id = '81'
and style_id = 'Y186F3D'
and color_id = '035Y'
and property_mark = '8FQR'
group by
style_id
,color_id
,size_id
,in_whse_date
order by size_id
)
select * from assign_data
pivot XML (
sum(qty) for (in_whse_date) in ( select distinct in_whse_date from assign_data)
)
Inner query produces this:
Pivot XML produces this:
Question: Is it possible to generate PIVOT with columns from a subquery that are not in xml format?
If not, is there another way to simulate this pivot behavior?
I have two huge tables. Let's call them as ITEM table (1807236 records) and ITEM_PROD_DUMP table (796369 records).
I need to update two columns (total_volume_amount, total_volume_uom) from ITEM table with the values of second table ITEM_PROD_DUMP where their primary key (SYS_ITEM_ID) matches.
I have written a query to do so, it works but only for handful records. For these huge number of records, it just keeps on running.
Can anyone please help me to write a correct and optimal query.
Query I have written:
update item i set i.total_volume_amount = (select ipd.total_volume_amount
from item_prod_dump ipd
where i.sys_item_id = ipd.sys_item_id),
i.total_volume_uom = (select ipd.total_volume_uom
from item_prod_dump ipd
where i.sys_item_id = ipd.sys_item_id)
where exists (select ipd.total_volume_amount
from item_prod_dump ipd
where i.sys_item_id = ipd.sys_item_id);
Use a MERGE statement. Pure and simple. 1.8 million records is not a "huge" number of records.
merge into item t
using ( SELECT *
FROM item_prod_dump ipd ) u
on ( t.sys_item_id = u.sys_item_id )
when matched then update set t.total_volume_amount = u.total_volume_amount,
t.total_volume_uom = u.total_volume_uom;
I've spend hours pouring over documentation on SUMMARIZE, SUMMARIZECOLUMNS, ADDCOLUMNS AND TOPN, and I just cannot get this simple calculation to come out correctly. I've even looked at results with DAX Studio, and it's always wrong. I'm trying to do this the "right way," i.e., without using deprecated techniques like using SUMMARIZE to add columns. The problem is simple and classic: summarize a sales table by Salesman, and add up the sales for the top N. Here's some data. I presume I need to calculate an intermediate summary table -- but maybe that's not necessary?
If someone could just layout the DAX code, I'd greatly appreciate it! I've tried everything!
Here's my code:
Top N Acc Rev.. =
var N = MAX( 'Numbers'[RankNum] )
VAR CaseTable = SaleData
VAR Table0 = SUMMARIZE( CaseTable, [Salesman], [Sale])
VAR Table1 = TOPN(N, Table0, [Salesman], DESC)
RETURN
SUMX( Table1, [Sale] )
The data wasn't as complex as it should be. I also had a mistake of ranking by Salesman versus ranking by Salesman's total sales in the TOPN function. So here is my new data and code which finally works. The results for N = 1 or 2 are 625 and 1127. Thanks, Jos Wooley!
SaleTest =
VAR N = MAX(Numbers[RankNum])
VAR CaseTable = Filter(SalesData, [Sale] > 100 )
VAR Table0 = SUMMARIZE(CaseTable, [Salesman], "Sales", SUM( SalesData[Sale] ) )
VAR Table1 = TOPN(N, Table0, [Sales], DESC)
RETURN SUMX( Table1, [Sales] )
i need help with the following dax statement.
Situation:
I have 2 tables. One table contains sell data with articleIDs, dateIDs and sell prices, another table contains stock movements data with articleIDs, dateIDs and purchase prices. According to the dateID i want to write the purchase prices into the first table using a calculated column because i need the prices for every row.
Example:
Table1 t1
t1.articleID = 123; t1.dateID = 20160905; t1.sellPrice = 62,55; t1.purchasePrice = My DAX Statement
Table2 t2
t2.articleID = 123; t2.dateID = 20160905; t2.purchasePrice = 37,07
t2.articleID = 123; t2.dateID = 20160905; t2.purchasePrice = 37,07
t2.articleID = 123; t2.dateID = 20160906; t2.purchasePrice = 37,07
t2.articleID = 456; t2.dateID = 20160905; t2.purchasePrice = 12,15
My DAX Statement:
= CALCULATE (
VALUES (t2[purchasePrice]);
TOPN (
1;
FILTER(FILTER(t2; t2[articleID] = t1[articleID]); t2[dateID] <= t1[dateID]); t2[dateID]; DESC
)
)
With my DAX Statement i get the following error:
A table of multiple values was supplied where a single value was expected.
It is normal that i have more than one row matching in the table 2.
Actually I just want the price of any of them on the corresponding dateID, even if they are tied. So i used the TOPN function with the value 1 and sorted by date but the error still remains. Is there a way to fix my DAX Statement to achieve this?
Create a calculated column in T1 and use this expression:
purchasePrice =
CALCULATE (
MAX ( T2[purchasePrice] ),
FILTER ( T2, T1[ArticleID] = T2[articleID] && T1[DateID] = T2[dateID] )
)
Note I use comma to separate passed arguments to the functions but I see in your expression you used semicolon. Change it to match your system list separator.
It is not tested but should work. Let me know if it works for you.
The issue I face that I need after selecting multiple rows,to loop over each row and fetch those who have some related information to the first row.
Example:
select NAME,ENGLISH_GRADE,FRANCE_GRAE
from (some complex query that have order by and returns 100 rows) WHOLE_ROWS
where
//Here I need to loop over WHOLE_ROWS and make something like that:
//if(currentRow.ENGLISH_GRADE==WHOLE_ROWS(0).ENGLISH_GRADE)
//fetch this row
Basically, you need to join your query to itself. You can do with with a subquery factoring clause:
WITH complex_query AS
( ... complex query here ... )
SELECT
FROM complex_query cq1
WHERE cq1.english_grade = ( SELECT english_grade FROM cq1
WHERE rownum = 1 )
Here is a SQL Fiddle. You could also do this with analytics, but those seem to me more difficult to understand.