Hi,
I am trying to solve a DAX related quiz that i will try to explain translated to AdventureWorks DB. I am working on SSAS 2012 Tabular.
I load from SQL with following query:
select ProductID, OrderDateEOM, sum(OrderQty) SumOrderQty
from (
select
p.ProductID,
DATEADD(month, ((YEAR(soh.OrderDate) - 1900) * 12) + MONTH(soh.OrderDate), -1) as OrderDateEOM,
sod.OrderQty
from Production.Product p
join Sales.SalesOrderDetail sod on p.ProductID = sod.ProductID
join Sales.SalesOrderHeader soh on soh.SalesOrderID = sod.SalesOrderID
) as orderdetails
group by ProductID, OrderDateEOM
order by ProductID, OrderDateEOM
which brings an aggregate of Sales Quantity per month per product.
My resulting table has columns:
ProductID / OrderDateEOM / SumOrderQty
Now the goal is to add a calculated column which shows from an historical perspective the Maximum SumOrderQty for each OrderDateEOM per ProductID. Historical means that it should not show the overall maximum, but the maximum from begin of time up to the given OrderDateEOM.
I tried something like:
=MAXX(
FILTER(ProductSalesByDate,
ProductSalesByDate[ProductID]=707 && ProductSalesByDate[OrderDateEOM] <= "2007-08-31"
),
ProductSalesByDate[SumOrderQty]
)
Which works ok for the given parameters [ProductID]=707 and [OrderDateEOM]="2007-08-31". Now what i would like is that this same is evaluated in row context and the concrete values for [ProductID] and [OrderDateEOM] are taken from row context.
In other words, for each row iteration i would like that the full table ProductSalesByDate is sliced by the rows context [ProductID] and [OrderDateEOM] and that the MAXX function takes the maximum of this sliced table.
But i cannot get this to work when the referencing is done to the same table that is iterated. In pseudo-DAX i would write something like:
=MAXX(
FILTER(ProductSalesByDate,
ALL_ROWS(ProductSalesByDate)[ProductID]=CURRENT_ROW([ProductID]) && ALL_ROWS(ProductSalesByDate[OrderDateEOM]) <= CURRENT_ROW("2007-08-31")
),
ProductSalesByDate[SumOrderQty]
)
I guess there is some easy way to do this but i cant find out. I managed to do it by duplicating the tables and working with 2 related tables, one referencing the other, but there should be a simpler way to do it.
Any idea?
thanks, dv
I am trying to solve a DAX related quiz that i will try to explain translated to AdventureWorks DB. I am working on SSAS 2012 Tabular.
I load from SQL with following query:
select ProductID, OrderDateEOM, sum(OrderQty) SumOrderQty
from (
select
p.ProductID,
DATEADD(month, ((YEAR(soh.OrderDate) - 1900) * 12) + MONTH(soh.OrderDate), -1) as OrderDateEOM,
sod.OrderQty
from Production.Product p
join Sales.SalesOrderDetail sod on p.ProductID = sod.ProductID
join Sales.SalesOrderHeader soh on soh.SalesOrderID = sod.SalesOrderID
) as orderdetails
group by ProductID, OrderDateEOM
order by ProductID, OrderDateEOM
which brings an aggregate of Sales Quantity per month per product.
My resulting table has columns:
ProductID / OrderDateEOM / SumOrderQty
Now the goal is to add a calculated column which shows from an historical perspective the Maximum SumOrderQty for each OrderDateEOM per ProductID. Historical means that it should not show the overall maximum, but the maximum from begin of time up to the given OrderDateEOM.
I tried something like:
=MAXX(
FILTER(ProductSalesByDate,
ProductSalesByDate[ProductID]=707 && ProductSalesByDate[OrderDateEOM] <= "2007-08-31"
),
ProductSalesByDate[SumOrderQty]
)
Which works ok for the given parameters [ProductID]=707 and [OrderDateEOM]="2007-08-31". Now what i would like is that this same is evaluated in row context and the concrete values for [ProductID] and [OrderDateEOM] are taken from row context.
In other words, for each row iteration i would like that the full table ProductSalesByDate is sliced by the rows context [ProductID] and [OrderDateEOM] and that the MAXX function takes the maximum of this sliced table.
But i cannot get this to work when the referencing is done to the same table that is iterated. In pseudo-DAX i would write something like:
=MAXX(
FILTER(ProductSalesByDate,
ALL_ROWS(ProductSalesByDate)[ProductID]=CURRENT_ROW([ProductID]) && ALL_ROWS(ProductSalesByDate[OrderDateEOM]) <= CURRENT_ROW("2007-08-31")
),
ProductSalesByDate[SumOrderQty]
)
I guess there is some easy way to do this but i cant find out. I managed to do it by duplicating the tables and working with 2 related tables, one referencing the other, but there should be a simpler way to do it.
Any idea?
thanks, dv