Calculate new Power BI table from multiple tables using UNION, SUMMARIZE and FILTER - filter

Hi I am new to Power Bi and I am trying to generate a set of summary tables which combine data from multiple tables.
New Table= ADDCOLUMNS(
UNION(
SUMMARIZE(
FILTER(
'Purchased',
'Purchased'[Units] > 1
),
'Purchased'[Account],
"Total Units", SUM('Purchased'[Units])
),
SUMMARIZE(
FILTER(
'Users',
'Users'[Start Time].[MonthNo] = EOMONTH(TODAY(), -1)
),
'Users'[Account],
"Last Month", COUNT('Users'[Name])
)
),
"Last Month", COUNT('Users'[Name])
)
The result gives me a count of units by account, but each row is given the total of Users.
Account
Total Units
Last Month
Name 1
12
8000
Name 2
63
8000
Name 4
42
8000
How do I get the summarised result in my Last Month field?

Related

Dax Formula to filter out Previous records and only keep last transaction of the material number

I have a table of material in which I need to define the last costing of the item over time. I need the table to filter all the old records per material number. I have tried to find the max date per record, but it's not really what I need.
There is table joins per over three tables, these tables are Average cost Lead-time, items to be cleaned-cat-ver2 and last purchased price.
I have tried
first attempt
second attempt
thanks in advance
Edit
Small example
table
I need to return line with latest costing date
I have tried
max Date =
CALCULATE (
MAX ( 'last Purchased Price'[Document Date].[Date] ),
FILTER (
'Items to be Cleaned-cat-veri 2',
'Items to be Cleaned-cat-veri 2'[material Number]
= EARLIER ( 'Items to be Cleaned-cat-veri 2'[material Number] )
)
)
and
Max Date 2 =
VAR currentitem = 'Items to be Cleaned-cat-veri 2'[material Number]
RETURN
MAXX (
FILTER (
ALL ( 'last Purchased Price'[Document Date] ),
'Items to be Cleaned-cat-veri 2'[material Number] = currentitem
),
( 'last Purchased Price'[Document Date] )
)

Dax calculated column for holding max value grouped by date

I have a table that holds ten days of values broken out by hour:
date hour sales
11/20/2019 1 10
11/20/2019 2 20
11/20/2019 3 30
...
11/20/2019 23 230
this pattern is repeated for each date until the current date where a row is inserted on the hour with latest sales
I would like to take the last sales amount for each date and divide the prior rows by that value using DAX. I am first trying to create a calculated column to just hold the max sales value, but for some reason I am getting the max value for the table instead of the group:
Estimated[SalesPct] =
var maxSales = MAXX('Estimated'[Exp_Sales])
return
CALCULATE(divide('Estimated'[Exp_Sales], maxSales)
For calculated column following DAX will do:
Estimated[SalesPct] =
VAR maxSales =
CALCULATE (
MAX ( 'Estimated'[Exp_Sales] ),
ALLEXCEPT ( 'Estimated', 'Estimated'[DateColumn] ) //change to your date column
)
RETURN
DIVIDE ( 'Estimated'[Exp_Sales], maxSales )
Thank

How to create a ranking in power bi matrix with DAX from a single table with aggregation

I'm trying to have a ranking of the supplier based on their cases sold from the last 12 months (MAT12_cs) in a matrix in Power BI.
Here is a sample data:
Table_sales
Supplier, Product, Account, Rep, MAT12_cs
Sup1, Prod1, Acc1, Rep1, 56
Sup1, Prod1, Acc2, Rep2, 45
Sup1, Prod2, Acc1, Rep1, 43
Sup1, Prod2, Acc2, Rep2, 66
Sup2, Prod3, Acc1, Rep1, 15
Sup2, Prod4, Acc3, Rep2, 104
Sup3, Prod5, Acc4, Rep3, 86
Sup3, Prod5, Acc1, Rep1, 80
Here is the result I'm expecting:
Supplier, MAT12_cs, Rank
Sup1, 210, 1
Sup3, 166, 2
Sup2, 119, 3
Total, 495
I tried RANKX in a measure:
Rank = RANKX(Table_sales,SUM(MAT12_CS))
It gives 1 everywhere.
I tried something like this but something is missing to make it work I think:
Rank =
VAR ProdSales = SUM('Table_sales'[MAT12_cs])
VAR tblSales =
SUMMARIZE (
'Table_sales',
'Table_sales'[Supplier],
"Total Sales", SUM ( 'Table_sales'[MAT12_cs] )
)
RETURN
IF(ProdSales>0,COUNTROWS(FILTER(tblSales,[Total Sales]>ProdSales))+1,BLANK())
This gives me totals I don't by what I should replace countrows with to have a ranking.
Create a measure (I am calling your table "Sales" for short):
Total Sale = SUM ( Sales[MAT12_cs] )
Create another measure:
Sale Rank =
IF (
HASONEVALUE ( Sales[Supplier] ),
RANKX ( ALL ( Sales[Supplier] ), [Total Sale] )
)
Put these measures into a matrix or table against suppliers. Result:
Explanation:
You must use ALL(Table) instead of just 'Table' in RANKX. Without ALL, RANKX will not see the entire data (as it must, to rank all sales), it will only see filtered table. For example, in the first row, you will only see sales for supplier 1, because your table "Sales" is filtered in this row by Sup1. As a result, RANKX is ranking just one record, that's why you are getting 1s in each line. When we use ALL, RANKX will (correctly) see all data.
After getting access to all suppliers, RANKX iterates them one by one, and for each supplier calculates their sales and then ranks them.
HASONEVALUE part is needed to remove ranking from the totals.

DAX [SSAS Tabular] DISTINCTCOUNT with lastnonempty date

I am analysing product distribution data.
We want a snapshot measurement of market penetration [Depth].
Depth := DIVIDE (
[Count of ranged product/store distribution points],
[Count of audited product/store distribution points]
)
I have 2 tables:
'Distribution' (audited distribution points); and
'Calendar' (a date table).
They are related in the model.
For a snapshot at 30 June 2015, I don't want to include new product/stores from November 15, but I do want include any data points that were audited in the prior 3 months.
Logically the numerator is a filtered subset of the denominator so I need the denominator first, which is where I am stuck.
If I just do a basic distinctcount without any fancy code I get this.
Month, Denominator
Mar-15, 1
Apr-15, 0
May-15, 0
Jun-15, 2
Jul-15, 6
Aug-15, 5
Sep-15, 1
Oct-15, 40
Nov-15, 53
Dec-15, 92
But I want something that looks like this:
Month, Denominator
Mar-15, 150
Apr-15, 150
May-15, 150
Jun-15, 170 -- add 1 new product in 20 stores
Jul-15, 170
Aug-15, 170
Sep-15, 170
Oct-15, 200 -- add 1 New product in 30 stores
Nov-15, 200
Dec-15, 200
I need to drop the filter context on Distribution[Date] and apply a filter on Distribution of Distribution[Date] <= Calendar[Date] and then do the distinct count but I get errors.
Count of audited product/store distribution points:=
CALCULATE(
COUNTROWS (
VALUES ( Distribution[ProductStoreKey])
),
NOT ( Distribution[Status On Exit] = "Ineligible" ),
FILTER (
ALL ( Distribution[Date] ),
Distribution[Date] <= Calendar[Date]
)
)
ERROR:
The value for column 'Date' in table 'Distribution' cannot be determined in
the current context. Check that all columns referenced in the calculation
expression exist, and that there are no circular dependencies. This can also
occur when the formula for a measure refers directly to a column without
performing any aggregation--such as sum, average, or count--on that column.
The column does not have a single value; it has many values, one for each
row of the table, and no row has been specified.
It might be a error-proofing of the filter using HASONEVAUE. I'm not sure.
Among other ideas, I've tried rewriting the filter but this doesn't work either.
FILTER (
Distribution,
Distribution[Date] <=
CALCULATE (
MAX(Distribution[Date]),
Distribution[Date]<=Calendar[Date]
)
)
Error:
The expression contains multiple columns, but only a single column can be
used in a True/False expression that is used as a table filter expression.
This code gets the DistibutionDate of the last datapoint at variable Calendar[Date] but I cant figure out how to incorporate it.
Last Ever Dist Date:=
CALCULATE (
LASTDATE( Distribution[DATE] ),
DATESBETWEEN(
Calendar[date],
BLANK(),
LASTDATE(Calendar[Date])
),
ALL(Calendar)
)
How about:
Count of audited product/store distribution points:=
CALCULATE(
DISTINCTCOUNT(Distribution[ProductStoreKey]),
DATESBETWEEN(
Calendar[date],
BLANK(),
LASTDATE(Calendar[Date])
),
ALL(Calendar)
)

Running Total without any date filters in DAX

I am trying to get a running total calculation for a set of data that does not have any Dates. I have a column with Product Categories and a column with Sales Dollars for each Product Category from the Fact Table. I need to add a running total column as shown below. I am new to DAX and looking for some help on calculating a running total.
Category Sales $ Cumulative Sales $
FOOD SERVICE 9051 9051
HOT FOOD 1880 10931
GRILL 1815 12746
FRESH SANDWICHES 1189 13935
FRESH BAKERY 1100 15035
PACKAGED BAKERY 1074 16109
COLD SNACKS 645 16754
FAST FOOD 388 17142
FRESH BAKERY MULTI-DAY 252 17394
ENTREES/SALAD 180 17574
NACHOS 126 17700
BREAD 120 17820
Grand Total 17820
I think you have to have it ordered by something. If you want it ordered by Sales as you have it above you could create a calculated column to store the order as follows:
= RANKX ( ALL ( Table1 ), [Sales], [Sales] )
Then create a new calculated column for your cummulative total:
=
CALCULATE (
SUM ( [Sales] ),
FILTER (
ALL ( Table1 ),
[CalculatedColumn2] <= EARLIER ( Table1[CalculatedColumn2] )
)
)

Resources