Moving measure result to another dimension in DAX - dax

I need create a measure that takes the sum of sales of certain products and then shows that sum on a row with a different product.
I can get the desired sum as follows: Sales of A:=CALCULATE([Sales], 'Product'[Product] = "Product A")
Now I need to show that amount where the product is "Product B" (and the Customer is the same).
Product
Customer
Sales
Sales of A
Moved Sales
Product A
ABC
10
10
Product A
DEF
20
20
Product B
ABC
30
10
10
Product B
DEF
40
20
20
Product C
ABC
50
10
A simple Moved Sales:=CALCULATE([Sales of A], KEEPFILTERS('Product'[Product] = "Product B")) does not work because the filter is removed by the inner calculation.

I'd try something like this:
Moved Sales:=
IF(
SELECTEDVALUE('Product'[Product])="Product B"
,[Sales of A]
,BLANK()
)

Hello You can define such a measure:
Moved Sales =
VAR SalesOfA =
ADDCOLUMNS (
TblProduct,
"Sales Of A",
SWITCH (
TblProduct[Product],
"Product A", CALCULATE ( TblProduct[Sales], TblProduct[Product] = "Product A" ),
"Product B", CALCULATE ( TblProduct[Sales], TblProduct[Product] = "Product B" ),
CALCULATE ( TblProduct[Sales], TblProduct[Product] = "Product C" )
)
)
VAR Change_B_With_A =
ADDCOLUMNS (
SalesOfA,
"B_With_A",
SWITCH (
TblProduct[Product],
"Product A", BLANK (),
"Product B", "Product A",
BLANK ()
)
)
VAR Moved_Sales =
ADDCOLUMNS (
Change_B_With_A,
"Moved Sales",
IF (
AND ( [B_With_A] = "Product A", TblProduct[Customer] = "ABC" ),
CALCULATE (
[Sales],
ALL ( TblProduct ),
TblProduct[Product] = "Product A",
TblProduct[Customer] = "ABC"
),
IF (
AND ( [B_With_A] = "Product A", TblProduct[Customer] = "DEF" ),
CALCULATE (
[Sales],
ALL ( TblProduct ),
TblProduct[Product] = "Product A",
TblProduct[Customer] = "DEF"
)
)
)
)
VAR Result =
SUMX ( Moved_Sales, [Moved Sales] )
RETURN
Result
Step By Step Screenshots:
And last step to define [Moved Sales] measure :
You can slice and dice it.
Moved Sales =
...
...
...
VAR Result = SUMX(Moved_Sales, [Moved Sales])
RETURN
Result

Related

Counting customers that have sales in the same products

The data table has customers, products sales etc.
Based on a slicer selection products i want to count how many customers have sales in all selected products.
As below i only want to count customer B because he is the only one having all selected products
Customer a Product a product b product c
A. 1 1
B. 1 1 1
C. 1
D. 1 1
E. 1 1
OKay. I think Your table is this:
Then You need this dax code:
CustomerHavingAll3Products =
VAR Onlya =
CALCULATETABLE ( VALUES ( 'Product'[Customer] ), 'Product'[Product a] <> 0 )
VAR Onlyb =
CALCULATETABLE ( VALUES ( 'Product'[Customer] ), 'Product'[Product b] <> 0 )
VAR Onlyc =
CALCULATETABLE ( VALUES ( 'Product'[Customer] ), 'Product'[Product c] <> 0 )
VAR CombinedAll =
INTERSECT ( INTERSECT ( Onlya, Onlyb ), Onlyc )
RETURN
COUNTX ( CombinedAll, [Customer] )
If we test it on a table visual:
Please do not forget to click the down-pointing arrow on customer column on the filter pane, and ensure that "show items with no data" is checked, see the picture below
you can do it on power query side... totally dynamic...
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIEYjAVqxOt5AQVMISLOMOkgRSI74KuxRVJi1JsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, #"Product a" = _t, #"product b" = _t, #"product c" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Product a", Int64.Type}, {"product b", Int64.Type}, {"product c", Int64.Type}}),
count_columns = Table.ColumnCount(Source)-1,
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Customer"}, "Attribute", "Value"),
#"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Customer"}, {{"Total Count", each Table.RowCount(_), Int64.Type}}),
#"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each [Total Count] = count_columns )
in
#"Filtered Rows"

How to get DISTINCTCOUNT in GROUPBY table

I have a table 'Report' which contain records of inspection from [StartDate] to [EndDate].
Inspections don't appear every day, so I have [ReportDate] too, but in one day I can have a few inspections.
My table 'Report' has column: A, B, C, StartDate, EndDate, ReportDate.
I want to receive grouping table which will contain number of days when inspection took place.
This number of days should summary DISTINCTCOUNT of [ReportDate].
My problem is that I can not use DISTINCTCOUNT inside of GROUPBY().
So how I can calculate DISTINCTCOUNT?
How to change below DAX statement?
New Table =
GROUPBY('Report'
,'Report'[A]
,'Report'[B]
,"Start Time", MIN ( CURRENTGROUP(), 'Report'[StartDate] )
,"End Time", MAXX ( CURRENTGROUP(), 'Report'[EndDate] )
,"Days", COUNTAX ( CURRENTGROUP(), 'Report'[ReportDate] )
)
Switch to SUMMARIZE:
New Table =
SUMMARIZE(
'Report',
'Report'[A],
'Report'[B],
"Start Time", MIN( 'Report'[StartDate] ),
"End Time", MAX( 'Report'[EndDate] ),
"Days", COUNTA( 'Report'[ReportDate] ),
"Distinct Days", DISTINCTCOUNT( Report[ReportDate] )
)
You directed me to a different solution.
I used twice GROUPBY statement and receive a proper value.
New Table =
GROUPBY(
GROUPBY('Report'
,'Report'[A]
,'Report'[B]
,'Report'[ReportDate]
,"Start Time", MIN ( CURRENTGROUP(), 'Report'[StartDate] )
,"End Time", MAXX ( CURRENTGROUP(), 'Report'[EndDate] )
)
,'Report'[A]
,'Report'[B]
,"Start Time", MIN ( CURRENTGROUP(), [StartDate] )
,"End Time", MAXX ( CURRENTGROUP(), [EndDate] )
,"Days", COUNTAX ( CURRENTGROUP(), [ReportDate] )
)

Based on a value show different message

Follow Up :
I have these two tables that are mutually exclusive (not connected in any way) .
The first table has date , number of customers on the dayDISTINCTCOUNT(sales[user_name]), total sales , tier (text - will explain)
The second table is CustomerLimit which is basically consecutive numbers between 1 and 100.
Used the tier measure as the answer below (thank you)
Tier =
VAR Limit = SELECTEDVALUE ( CustomerLimit[CustomerLimit] )
VAR CustCount = COUNT ( Customers[CustomerID] )
RETURN
IF (
ISBLANK ( Limit ), "Select a value",
IF ( CustCount > Limit, "Good", "Bad" )
)
Now I need to aggregate the total amount of customers by Tier.
I used
calculate(DISTINCTCOUNT(sales[user_name]),Tier = "Good") .
It give me an error of : A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed.
Is that possible ?
You can capture the limit using SELECTEDVALUE and then compare.
Tier =
VAR Limit = SELECTEDVALUE ( CustomerLimit[CustomerLimit] )
VAR CustCount = COUNT ( Customers[CustomerID] )
RETURN
IF (
ISBLANK ( Limit ), "Select a value",
IF ( CustCount > Limit, "Good", "Bad" )
)

Count unique matching items with filter as a calculated column

I have two tables are Data and Report.
Data Table:
In Data table contain three columns are Item, status, and filter.
The item contains duplicated entry and the item column contains text and number or number only or text only.
The status column contains two different text/comments, "Okay" and "Not Okay"
The filter column contains two different filters which are A1 and A2.
The report table
In the Report table, I updated both comments/text as "Okay" or "Not Okay". I am looking for count against filter A1 and A2 according to the comments.
I would like to create a new calculated column in the report table in order to get the unique count according to the comments and filter based on the data table columns item and status.
DATA:
REPORT
Alexis Olson helped the following calculated column in order to get the unique count. I am trying to add one more filter in existing DAX calculated column but it's not working. Can you please advise?
1.Desired Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment
)
2.Desired Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] ),
DATA[ITEM]
)
)
3.Desired Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF ( CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS], 1, 0 )
)
I think you can just add a filter to CALCULATE:
Filter by A1 Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment,
DATA[FILTER] = "A1"
)
For the second method,
Filter by A1 Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] && REPORT[FILTER] = "A1" ),
DATA[ITEM]
)
)
I do not recommend using the third one but it would be like this
Filter by A1 Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF (
CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS]
&& CALCULATE ( SELECTEDVALUE ( DATA[FILTER] ) ) = "A1",
1,
0
)
)

Function 'SAMEPERIODLASTYEAR' expects a contiguous selection Issue

I have a tabular data model in Visual Studio, I made a formula to get last year's sales over a given period:
Selected Measure LY:= CALCULATE([Selected Measure], SAMEPERIODLASTYEAR('date' [date]))
I made a formula to have the decomposition of all the months in the 'date' table:
FY_Month='date' [FY Month No]&'date' [Month_label]
And:
FY_Month_No=format(if('date'[month_no]>6,'date'[month_no]-6,'date'[month_no]+6), "00")
Month_label : all the months of the year
The "Selected Measure LY" display works fine when I have all months selected but when I remove one I get this error message:
Calculation error in "Selected Measure LY": the "SAMEPERIODLASTYEAR" function expects a contiguous selection when the date column comes from a tabkle on side 1 of a bidirectional relationship.
Between my 'date' table and my sales table I have a 1/N relationship, I tried to change this relationship too but the error always comes back.
I looked for solutions on forums and I found this but I don't find the same numbers as with my first formula:
Selected Measure LY:=
CALCULATE([Selected Measure],
FILTER (
ALL ( 'date' ),
YEAR ( 'date'[date] ) = YEAR ( TODAY () )
&& 'date'[date] <= TODAY ()
)
)
My code for Selected Measure:
Selected Measure:=
VAR hasFilter =
HASONEFILTER ( 'Measure Selection'[Measure Name] )
VAR selMeas =
SELECTEDVALUE ( 'Measure Selection'[Measure Name], BLANK () ) /*SWITCH works slow, so using many IFs*/
VAR preCalc =
IF (
selMeas = "Units",
[Sku Piece Quantity],
IF (
selMeas = "Net Sales",
[Euro Net Amount],
IF (
selMeas = "Gross Sales",
[Euro Gross Amount],
IF (
selMeas = "Average Wholesale",
[Average_Wholesale],
IF (
selMeas = "Average Units",
[Average Units],
IF (
selMeas = "No of Styles",
[Style Count],
IF (
selMeas = "No of Colours",
[Colour Count],
IF (
selMeas = "ROD by Style",
[ROD by Style],
IF (
selMeas = "Standard Cost",
[Sum_Cost_Base],
IF (
selMeas = "Net Margin",
[Net_Margin],
IF (
selMeas = "Gross Margin",
[Gross_Margin],
IF ( selMeas = "ROD by Colour", [ROD by Colour], BLANK () )
)
)
)
)
)
)
)
)
)
)
)
VAR calc =
IF ( hasFilter, preCalc, BLANK () )
RETURN
calc
How can I correct it?

Resources