Below is my MDX query. Here I have applied sorting for one column, how can I sort all the columns, the reason behind this is, I no column should have any values like (null), null, empty, unknown etc.
WITH MEMBER PageSize AS
1
MEMBER [PageNumber] AS
1
MEMBER [Measures].[Orderby Measure] AS
[Measures].[Budget Delivered COGS]
MEMBER [Orderby] AS
"BASC"
SET ROWAXISWOF AS
NonEmpty (
( [Time Periods].[Fiscal Year].[Fiscal Year].Members, [Time Periods].[Fiscal Quarter].[Fiscal Quarter].Members, [Time Periods].[Fiscal Month].[Fiscal Month].Members, [Time Periods].[Fiscal Week].[Fiscal Week].Members ),
{
{ [Measures].[Budget Delivered COGS] },
{ [Measures].[Break Even Delivered] }
}
)
SET ROWAXISWF AS
Filter ( ROWAXISWOF, [Measures].[Budget Delivered COGS] )
SET ROWAXIS AS
IIf ( 'Filter' = "NotFilter", ROWAXISWF, ROWAXISWOF )
MEMBER [Measures].[MaxRowCount] AS
ROWAXIS .Count
SET ROWAXIS_Count AS
IIf (
'Paging' = "Paging",
(
CASE
WHEN [Orderby] = 'BASC'
THEN Tail (
TopCount ( ROWAXIS, PageSize * PageNumber, [Measures].[Orderby Measure] ),
PageSize
)
ELSE Tail (
BottomCount ( ROWAXIS, PageSize * PageNumber, [Measures].[Orderby Measure] ),
PageSize
) END ),
ROWAXISWOF
)
SELECT ( IIf (
'Paging' = "Paging",
(
{ [Measures].[Budget Delivered COGS], [Measures].[Break Even Delivered], [Measures].[MaxRowCount] } ),
{ [Measures].[Budget Delivered COGS], [Measures].[Break Even Delivered] }
) ) ON COLUMNS,
ROWAXIS_Count ON ROWS
FROM (
SELECT (
{ [Time Periods].[Fiscal Year].&[2011], [Time Periods].[Fiscal Year].&[2012], [Time Periods].[Fiscal Year].&[2013], [Time Periods].[Fiscal Year].&[2014], [Time Periods].[Fiscal Year].&[2015] } ) ON COLUMNS
FROM [Homestore Sales]
)
It will return the output as preceding.
In that query for the Break Even Delivered header it is showing (null) as the first value . How can we make the value which are not null to be on top? if that is for one column we can just apply a sorting. My requirement is, no column should have null values in the first cell if any of the cell contains values. Please suggest me any idea. Thanks in advance.
I have done it by myself. I have changed the ROWAXISWOF as follows.
SET ROWAXISWOF AS
NonEmpty (
NonEmpty (
(
[Time Periods].[Fiscal Year].[Fiscal Year].Members, [Time Periods].[Fiscal Quarter].[Fiscal Quarter].Members, [Time Periods].[Fiscal Month].[Fiscal Month].Members, [Time Periods].[Fiscal Week].[Fiscal Week].Members
),
{ [Measures].[Budget Delivered COGS] }),
{ [Measures].[Break Even Delivered] } )
I have included NonEmpty conditions for each measures. Hope this will help some one.
Related
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] )
)
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]
)
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 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?
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