How to translate a Tableau FIXED LOD to Power BI? - powerbi

I have a FIXED LOD with multiple dimensions that looks likes this:
{ FIXED [Customer ID], [Quarter], [Product Type] : SUM([Sales]) }
In Power BI I have wrote it as below using the example given in this article:
CALCULATE(
SUM('Table'[Sales]),
ALLEXCEPT('Table', 'Table'[Customer ID]),
ALLEXCEPT('Table', 'Table'[Quarter]),
ALLEXCEPT('Table', 'Table'[Product Type])
)
However it is not working as expected, could you help me understand the correct way of doing it?

I found the answer by editing my original expression, the following worked as expected:
CALCULATE(
SUM('Table'[Sales]),
ALLEXCEPT('Table', 'Table'[Customer ID], 'Table'[Quarter], 'Table'[Product Type])
)

Related

How can I lock a measure in PBI?

Im working on a dashboard in PBI and I have the next problem...
I have a card that only have to show the month data. For this I use this measure:
Month Orders = CALCULATE(
DISTINCTCOUNT( 'Data'[ORDER] ),FILTER('Data',
'Data'[Month] = 'Data'[Actual Month]),
ALL('Data Incidentes')
)
I understand that if I use All() filter on a measure, this measure don't change when I apply a visual filter but in my case this change.
So my problem is that I have the card that show the month orders but when i apply a visual filter this data change and I don't want it to change.
I hope you understand my problem and my english jaja.
Month Orders = CALCULATE(
DISTINCTCOUNT( 'Data'[ORDER] ),FILTER('Data',
'Data'[Month] = 'Data'[Actual Month]),
ALL('Data Incidentes')
)
Can you try this instead-
Month Orders =
CALCULATE(
DISTINCTCOUNT( 'Data'[ORDER] ),
FILTER(
ALL('Data'),
'Data'[Month] = 'Data'[Actual Month]
)
)

DAX GROUPBY CURRENTGROUP

I have a table which is created as follows, which calculates 2 x measures - [SubTot Net Sales] and [SubTot Volume], however I wish to add a 3rd measure - being [SubTot Net Sales]/[SubTot Volume], however I can't find out how to accomplish this.
I have effectively tried DIVIDE(GROUPBY tbl_Act[Net Sales], GROUPBY tbl_Act[Volume] - (i.e same code as below) but I get error message:
"Function 'GROUPBY' scalar expressions have to be Aggregation functions over CurrentGroup(). The expression of each Aggregation has to be either a constant or directly reference the columns in CurrentGroup()."
Thoughts/Help much appreciated !!
Groupby = GROUPBY(
tbl_Act,
tbl_Act[Country],
tbl_Act[Channel],
tbl_Act[Customer],
tbl_Act[Category],
tbl_Act[Brand],
tbl_Act[Pack],
"SubTot Net Sales",
SUMX(
CURRENTGROUP(),
tbl_Act[Net Sales]),
"SubTot Volume",
SUMX(
CURRENTGROUP(),
tbl_Act[Volume]))
Just use ADDCOLUMNS:
Groupby =
ADDCOLUMNS(
GROUPBY(
tbl_Act,
tbl_Act[Country],
tbl_Act[Channel],
tbl_Act[Customer],
tbl_Act[Category],
tbl_Act[Brand],
tbl_Act[Pack],
"SubTot Net Sales", SUMX( CURRENTGROUP(), tbl_Act[Net Sales] ),
"SubTot Volume", SUMX( CURRENTGROUP(), tbl_Act[Volume] )
),
"SubTol Net Sales/Volume", DIVIDE( [SubTot Net Sales], [SubTot Volume] )
)

Calculate Ratio of Current and Previous Week per Task Type

I am fairly new to PowerBI and coming form an R background, I have some difficulties understanding how PowerBI decides in which context a measure is evaluated.
I have the following measures, which calculates the ratio between the previous and the current week:
AHT = DIVIDE(SUM('Daily Tasks'[SHT]), SUM('Daily Tasks'[#Tasks]))
AHT Current Week = CALCULATE([AHT], 'Dates'[Date] >= TODAY() - 7)
AHT Previous Week = CALCULATE([AHT], 'Dates'[Date] < TODAY() - 7 && 'Dates'[Date] >= TODAY() - 14)
AHT Ratio Week = DIVIDE([AHT Current Week], [AHT Previous Week])
So far so good. Now I want to display the smallest ratio in a card visual (together with the task type which features this ratio) . Thus, I created the following 2 measures:
Top of the Week % =
MAXX(
TOPN(
1,
SUMMARIZECOLUMNS(
'Daily Tasks'[Task Type],
"Ratio", [AHT Ratio Week]
),
[Ratio], ASC
),
[Ratio] - 1
)
Top of the Week =
CONCATENATEX(
TOPN(
1,
SUMMARIZECOLUMNS(
'Daily Tasks'[Task Type],
"Ratio", [AHT Ratio Week]
),
[Ratio], ASC
),
[Task Type]," - "
)
The visual shows the correct values and all seems fine. However, if I either select a task type in any of the other visuals, or use a slicer to filter but a single task type, the card visual shows an error saying that
SummarizeColumns and AddMissingItems must not be used in this context
So apparently something is amiss. How can I fix that?
SUMMARIZECOLUMNS does not support evaluation within a context transition. This makes it almost impossible to use in a measure. You will want to use Summarize instead.
Thanks to the hint of #Randy Minder I was able to solve the issue:
SUMMARIZE instead of SUMMARIZECOLUMNS did the trick.
I just needed to filter out BLANK values and ended up with the following code:
Top of the Week % =
MAXX(
TOPN(
1,
FILTER(
SUMMARIZE(
ALL('Daily Tasks'[Task Type]),
'Daily Tasks'[Task Type],
"Ratio",
[AHT Ratio Week]
),
NOT(ISBLANK([Ratio]))
),
[Ratio],
ASC
),
[Ratio] - 1
)

Dax function DATESINPERIOD not giving correct answer

I need to find moving two days sum of sales. I am using DAX function DatesinPeriod but the output is not coming correct. Please help me understand where I am going wrong please. I am using below Dax Formula:
Measure = CALCULATE(sum('Table'[Sale]),DATESINPERIOD('Dim Date'[Date],SELECTEDVALUE('Table'[Date]),-2,day))
To replicate the scenario first step is to create Dim Date table using - >
Dim Date = GENERATESERIES(date(2019,01,01),date(2019,12,31),1)
second Step is to create DataTable ->
Table = DATATABLE("Date",DATETIME,"Flag1",STRING,"Flag2",STRING,"Sale",INTEGER,{
{"8/1/2019","True","True",200},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"9/3/2019","False","True",60},
{"9/4/2019","False","True",10},
{"9/5/2019","False","True",100},
{"9/6/2019","False","True",30},
{"9/7/2019","False","True",60},
{"9/8/2019","False","False",150},
{"9/9/2019","False","False",80},
{"9/10/2019","False","False",90},
{"9/11/2019","False","False",30},
{"9/12/2019","False","False",20},
{"10/13/2019","False","True",50},
{"10/14/2019","False","True",60},
{"10/15/2019",BLANK(),BLANK(),BLANK()},
{"10/16/2019",BLANK(),BLANK(),BLANK()}
})
3rd Step - create a relation between these tables on date column
4th step - create Measure using - Measure = CALCULATE(sum('Table'[Sale]),DATESINPERIOD('Dim Date'[Date],SELECTEDVALUE('Table'[Date]),-2,day))
You will see the output coming wrong. see the screenshot. This is very strange. I tried using DatesBetween function , its also giving me the same wrong output.
Use the following measure to obtain the expected result:
SumInRange =
VAR __selectedDate = SELECTEDVALUE( 'Table'[Date] )
VAR __subTable =
FILTER(
ALL( 'Table'[Date] ),
AND(
'Table'[Date] >= __selectedDate -2,
'Table'[Date] <=__selectedDate
)
)
Return
CALCULATE(
SUMX (
DISTINCT ( 'Table'[Date] ),
CALCULATE ( MAX ( 'Table'[Sale] ) )
),
__subTable
)
Be sure to use the Date column from Table instead of the Dim in the visualization.

Dynamic DAX Number Format

I want to dynamically change the number format of a DAX measure, based on a dimension value (or indeed, based on the order of magnitude of the measure value).
I understand I can use SWITCH and FORMAT, as demonstrated by Kaspar De Jonge here: https://www.kasperonbi.com/dynamic-format-using-dax/
Here's an example of the type of measure I'm creating:
My Measure:=IF (
HASONEVALUE ( dimMeasureType[Measure Type] ),
SWITCH ( VALUES ( dimMeasureType[Measure Type] ),
"Total Cost", FORMAT ( [Total Cost], "#,##0, k" ),
"Cost Per Unit", FORMAT ( [Cost Per Unit], "#,##0.00" ),
"Cost % Sales", FORMAT ( [Cost % Sales], "0.00%" ),
BLANK()
),
BLANK()
)
But this technique returns text measures. I need to be able to chart my measures, so I do not want to convert them to text. Is there another technique for dynamically changing a measure number format, without converting to a string?
If it makes a difference, I'm working in SSAS-Tabular on SQL Server 2016 BI.
I don't believe this is currently possible, but it a popular feature request that will hopefully be implemented in the future.
I recommend voting and commenting on the idea I linked to in order to add your support.
A workaround is to create multiple measures and add them all to your chart. Depending on your dimension value only one measure returns values, all other measures return BLANK() and are not displayed in your chart. You can give them the same display name by adding whitespace to the end of their names:
My Measure:=IF (
SELECTEDVALUE( dimMeasureType[Measure Type] ) = "Total Cost",
[Total Cost],
BLANK()
)
[My Measure ]:=IF (
SELECTEDVALUE( dimMeasureType[Measure Type] ) = "Cost Per Unit",
[Cost Per Unit],
BLANK()
)
[My Measure ]:=IF (
SELECTEDVALUE( dimMeasureType[Measure Type] ) = "Cost % Sales",
[Cost % Sales],
BLANK()
)
This has some drawbacks though:
The chart legend shows all measures, even if all their values are BLANK().
The y-Axis of your chart has the same format as the first measure in its 'Values' section.