I have a star schema, of which fact_INV_DOC is the fact table ;
I would like to implement a new calculation group filter base on 2 tables linked to this fact table ;
I have tried this :
CALCULATE(
SELECTEDMEASURE() ,
FILTER(
fact_INV_DOC ,
NOT RELATED( dim_ITEMS_MASTERDATA[est_MDD] ) ||
RELATED( dim_PARTNER_GRP_LVL1[ID] ) = "10000912"
)
)
It works fine... except it blows up another running total filter I wish to use :
Running Year Total =
CALCULATE(
SELECTEDMEASURE() ,
FILTER(
ALL( dim_CALENDAR[DATE] ) ,
dim_CALENDAR[DATE] <= MAX(dim_CALENDAR[DATE])
) ,
VALUES('dim_CALENDAR'[YEAR])
)
How can I apply both my fact table filter and the running total calculation group?
Thank you for your help.
Related
I'm trying to use a DAX function to generate a table in Power BI. I have a fact table with Opened & Closed date columns and there is a requirement to report at the end of each day/month/year how many items were backlogged.
I've got the table to generate successfully with the code below - essentially joining the date and fact tables, however I can't then link it back to my dimensions due to a circular dependency error.
Researching it online suggests that I need to remove the blank row from fact_task_transaction with the ALLNOBLANKROW function. Unfortunately this has no effect.
Can anyone help?
Backlog Per Day =
var res = SELECTCOLUMNS (
GENERATE (
fact_task_transaction,
FILTER (
ALLNOBLANKROW ( 'Date' ),
AND(
'Date'[Date] >= fact_task_transaction[Opened At Date],
'Date'[Date] <= fact_task_transaction[Closed At Date]
)
)
),
"Date", 'Date'[Date],
"Task ID", fact_task_transaction[Task Id],
"Assignee ID", fact_task_transaction[Assignee Id]
)
return res
try this code - it only uses fact_task_transaction, so the joins with dimensions shuld be working fine
Backlog Per Day =
SELECTCOLUMNS (
GENERATE (
'fact_task_transaction',
GENERATESERIES (
CALCULATE ( MIN ( 'fact_task_transaction'[Opened At Date] ) ),
CALCULATE ( MAX ( 'fact_task_transaction'[Closed At Date] ) ),
1
)
),
"Date", [Value],
"Task ID", fact_task_transaction[Task Id],
"Assignee ID", fact_task_transaction[Assignee Id]
)
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" )
)
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
)
)
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
)
)
Presently my data returns:
What I need it to do is if the current month has 0's, it will default to the last months value with data:
I know this can be done with nested IF statements, but is there a better way?
UPDATED WITH #TPD SUGGESTION
The results from #TPD suggestion yield:
With measure defined as:
IF([Land Dev Alloc] = 0, CALCULATE([Land Dev Alloc],TOPN(1, CALCULATETABLE(Hyperion,FILTER(ALL(Hyperion), [Land Dev Alloc]>0)),Hyperion[DimDateID],DESC)),[Land Dev Alloc])
Where Hyperion is the main fact table that measure Land Dev Alloc pulls from
I'm not sure if this is the best way but I've recently solved a similar problem like this, assuming you have a Date table and a Value table joined with a relationship:
CurrentOrLastValue:=
CALCULATE (
-- EXTRACT VALUE FROM ROW
FIRSTNONBLANK( 'value'[value], 1 ),
-- FIRST ROW FROM YOUR 'VALUES' TABLE REDUCED TO THOSE BEFORE THE CURRENT CELL DATE
-- ORDERED BY DATE DESC
TOPN (
1,
CALCULATETABLE (
'value',
FILTER
(
ALL( 'date'[date] ),
'date'[date] <= MAX( 'date'[date] )
)
),
'value'[date],
DESC
)
)
Data tables and Pivot Table
Measure
Relationships
UPDATE TO SHOW MEASURE RESULT NOT RAW VALUE
Add a new core measure (doubling values to show a difference):
TotalValue:=SUM('value'[value]) * 2
Add a new measure to show desired output:
CurrentOrLastValueMeasure:=CALCULATE (
[TotalValue],
TOPN(
1,
CALCULATETABLE(
'value',
FILTER(
ALL( 'date'[date] ),
'date'[date] <= MAX( 'date'[date] )
)
),
'value'[date],
DESC
)
)
New measure in pivot table:
UPDATE TO SHOW LAST NON-ZERO VALUE WHEN MEASURE RETURNS ZERO
LastNonZeroMeasure:=
IF( [TotalValue] = 0,
CALCULATE (
[TotalValue],
TOPN(
1,
CALCULATETABLE(
'value',
FILTER(
ALL( 'value' ),
[TotalValue] > 0
)
),
'value'[date],
DESC
)
),
[TotalValue]
)
TotalValue not being doubled anymore. Two data points for 4th Jan to show the measure's aggregation working.
UPDATE TO IGNORE DATES AHEAD OF CELL DATE
Try filtering the dates also...
LastNonZeroMeasure:=IF( [TotalValue] = 0,
CALCULATE (
[TotalValue],
TOPN(
1,
CALCULATETABLE(
'value',
FILTER(
ALL( 'value' ),
[TotalValue] > 0
),
FILTER(
ALL( 'date' ),
'date'[date] < max( 'date'[date] )
)
),
'value'[date],
DESC
)
),
[TotalValue]
)
Ended up being far more simple than originally thought:
MeasureOne BALANCE YTD:= VAR LastNoneblankDate = CALCULATE(max('Date'[DimDateID]),FILTER(ALL('Date'),'Date'[Fiscal_Year] = MAX('Date'[Fiscal_Year])),FILTER(ALL(FactTable),[MeasureOne] > 0)) return IF([MeasureOne]=0, CALCULATE([MeasureOne],FILTER(ALL('Date'),'Date'[DimDateID] = LastNoneblankDate)), [MeasureOne BASE])
Where MeasureOne Base:
MeasureOne BASE:= VAR LastNoneblankDate = CALCULATE(max('Date'[Date]),FILTER(ALL(FactTable),[MeasureOne] > 0)) return IF(HASONEVALUE('Date'[Date]),[MeasureOne], CALCULATE([MeasureOne],FILTER(ALL('Date'),'Date'[Date] = LastNoneblankDate)))
the main issue was setting ALL(FactTable) instead of jut FactTable and handling the cases with