Based on a value show different message - filter

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" )
)

Related

How to count two column values

I want count two columns using power bi to create a visual.my measure as below
Test Measure = COUNTA('Export'[Line])+COUNTA('Export'[Line 2]),
You need to create a calculated table with all the lines values, like this
TableValues =
DISTINCT ( UNION ( DISTINCT ( 'Table'[Line] ), DISTINCT ( 'Table'[Line_2] ) ) )
With that table done, you can write the following measure
CountValues =
VAR _CurrentValue =
SELECTEDVALUE ( TableValues[Line] )
VAR _C1 =
CALCULATE ( COUNTA ( 'Table'[Line] ), 'Table'[Line] = _CurrentValue )
VAR _C2 =
CALCULATE ( COUNTA ( 'Table'[Line_2] ), 'Table'[Line_2] = _CurrentValue )
RETURN
_C1 + _C2
The calculations count all the instances of a specific Line. It's important that the TableValues doesn't have any relationship with the other tables.
Output
Using 'TableValues'[Line] as the Line and CountValues as the metric.

DAX Power Pivot: "= CONCATENATEX(CALCULATETABLE(" how to avoid duplicates?

I've seen similar posts before, but none of the solutions worked for me as my formula is complex and includes filters.
I'm trying to create a new column in a table, concatenating all matches from a column in a different table.(e.g. Order table concatenating all product names associated with a given order)
Full formula: =
CONCATENATEX (
CALCULATETABLE (
RoW_SIC_table,
FILTER (
RoW_SIC_table,
RoW_SIC_table[Excel Company ID] = Row_main[Excel Company ID]
),
FILTER ( RoW_SIC_table, NOT ( ISBLANK ( RoW_SIC_table[Broad] ) ) )
),
RoW_SIC_table[Broad],
", "
)
I want CONCATENATEX to only concatenate unique values, and I haven't made it work with either DISTINCT or VALUES. Can you help me?
How about like this?
Full formula: =
CONCATENATEX (
CALCULATETABLE (
DISTINCT ( RoW_SIC_table[Broad] ),
FILTER (
RoW_SIC_table,
RoW_SIC_table[Excel Company ID] = Row_main[Excel Company ID]
)
),
RoW_SIC_table[Broad],
", "
)
I used as reference this:
e.g. Order table concatenating all product names associated with a
given order
So what came to my mind is the following model:
SIC Table
Main Table
"Data Model"
Result
ProductsListedByOrderIFBroadISNOTBLANK=
VAR COrderID=SIC[OrderID]
VAR PIDWLineage=
SELECTCOLUMNS(
CALCULATETABLE(SIC,
ALL(SIC), SIC[OrderID]=COrderID, NOT(ISBLANK(SIC[Broad]))),
"ProductID", SIC[ProductID], "ProductName", RELATED(Main[ProductName]))
RETURN
CONCATENATEX(PIDWLineage, [ProductName], ",")

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
)
)

Distinct Count without using CALCULATE

I've come across this DAX measure:
# CustMultProds =
COUNTROWS(
FILTER(
Customer,
CALCULATE( DISTINCTCOUNT( Sales[ProductKey] ) ) >= 2
)
)
I pretty much understand how it works - it iterates over Customer inside the FILTER function, then the row context created by this iterator is transitioned into a filter context so that it is able to get the number of distinct products from the Sales table.
I am wondering is it possible to re-write the measure without using CALCULATE ? I got as far as using RELATEDTABLE but then not sure how to extract the distinct ProductKeys from each related table:
# CustMultProds =
COUNTROWS(
FILTER(
Customer,
RELATEDTABLE (Sales)
...
...
)
)
This is a possible implementation of the measure using RELATEDTABLE. But Context Transition still happens once per customer because RELATEDTABLE performs a context transition
# CustMultProds =
COUNTROWS(
FILTER(
Customer,
VAR CustomerSales =
RELATEDTABLE( Sales )
RETURN
MAXX( CustomerSales, Sales[ProductKey] )
<> MINX( CustomerSales, Sales[ProductKey] )
)
)
This is another way to write a measure leveraging RELATEDTABLE, that could be modified to deal with a different number of distinct products
# CustMultProds =
COUNTROWS(
FILTER(
Customer,
COUNTROWS( SUMMARIZE( RELATEDTABLE( Sales ), Sales[ProductKey] ) ) >= 2
)
)
This is another possible implementation, without CALCULATE and RELATEDTABLE.
But it scans the entire Sales table once per customer, so, even if it doesn't perform a context transition I'd expect it to be slower
# CustMultProds =
COUNTROWS(
FILTER(
Customer,
VAR SalesProducts =
SUMMARIZE( Sales, Sales[CustomerKey], Sales[ProductKey] )
RETURN
COUNTROWS(
FILTER( SalesProduct, Sales[CustomerKey] = Customer[CustomerKey] )
) >= 2
)
)

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