Dax function DATESINPERIOD not giving correct answer - powerbi
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.
Related
RANKX DAX not updating other graph
I've got the following DAX Measure that provides me with the ability to filter down to the last XX of activity from an individual. I can only seem to add the measure to the Filter on Visual so when chosing to filter down on say the last 10 this does not update other visuals in the report. What can I do so that I am able to view the last 10 activities, but for the other visuals to update? Rank = RANKX (ALLEXCEPT(Sheet1,Sheet1[Name]), CALCULATE(MAX(Sheet1[Date])),, DESC)
It's likely you are applying that filter to only one visual. It's better to implement the logic in the DAX calculation. DAX Calculation Rank = VAR _Calc = RANKX ( ALLEXCEPT ( Sheet1, Sheet1[Name] ), CALCULATE ( MAX ( Sheet1[Date] ) ), , DESC ) RETURN IF ( _Calc <= 10, _Calc )
Count the number of occurrences after associated date of certain value in Power BI
I'm conducting an exercise around examining test results after tutoring has occurred. Essentially looking at the rates of "pass" post tutoring within the context of a given student. Where the ultimate outcome would be: pass rate after tutoring = [count passes]/[count test date] WHERE test date > tutoring date. For example: Ideally, the final output of the measure would be = 1 (1/1) Would anyone be able to point me in the direction of achieving this through a Power BI measure? I've attempted the following to get the single oc with no luck: Measure 3 = CALCULATE(COUNT(Table[Test Pass?]),FILTER(Table,Table[Test Date]>CALCULATE(Min(Table[Tutoring Date]),FILTER(Table,Table[Tutor (?)] <> BLANK )))) Where I would then use the student column in a matrix with the measure to group pass rates post tutoring by student
I have used this simple flat table data model: You can calculate this with a measure that needs to be evaluated with your Student column: Pass Rate After Tutoring = VAR _tutor_date = CALCULATE ( MAX ( 'Table'[Tutoring Date] ), ALLEXCEPT ( 'Table', 'Table'[Student] ) ) VAR _tests_post_tutor = CALCULATE ( COUNTROWS ( 'Table' ), ALLEXCEPT ( 'Table', 'Table'[Student] ), 'Table'[Test Date] > _tutor_date ) VAR _successes = CALCULATE ( COUNTROWS ( 'Table' ), ALLEXCEPT ( 'Table', 'Table'[Student] ), 'Table'[Test Date] > _tutor_date, 'Table'[Test Pass] ) RETURN DIVIDE ( _successes, _tests_post_tutor ) But this assumes that students are only tutored for one specific test, and are tutored once.
power bi - ranking on a date
I tried to make a ranking on a date so that every beginning of a month it resets but I can't get it. the furthest I got is that every month simply gets an cumulative ranking based on RANKX on power bi which includes a month and a year. Has anyone needed such a ranking and succeeded?
A generalised set-up for a Calculated Column would be along the lines of: = VAR ThisMonth = MONTH( Table1[Date] ) RETURN RANKX( FILTER( Table1, MONTH( Table1[Date] ) = ThisMonth ), Table1[Date], , ASC )
Running total with a slicer in Powerbi
I am trying to find the running total by month, it worked using the following measure but only without using a slicer for the category "BLK": With Date = VAR MaxDate = MAX ( 'Calendar Table'[Date] ) RETURN CALCULATE ( SUM ( 'Injuries'[Total] ), FILTER ( ALL ( 'Injuries'[Classification] ), 'Injuries'[Classification] = "FAC" ), FILTER ( ALLSELECTED ( 'Calendar Table' ), 'Calendar Table'[Date] <= MaxDate) )+0 The result was only accurate when I don't apply the Category slicer "BLK", I tried a different measure: With BLK = CALCULATE( SUM('Oxy Oman Personal Injuries'[Total]), FILTER ( ALL ( 'Injuries'[Classification] ), 'Injuries'[Classification] = "FAC" ), ALLSELECTED('Blocks'[BLK]) )+0 The above gave me something similar to the actual data, it did not sum up the numbers.. at least it gave me the correct total of a the category selected = 5 while the first measure gave the total wrong = 4. Here is a screenshot of the results for both measures with and without a slicer: Without BLK slicer:(With Data gave me what I wanted) Using one Category in the BLK Slicer: How can I fix that?
Using TREATAS to get Measure from Another Table
I'm back with another issue. I have a sales table with transaction details on the products purchased. I also have a table with warehouse inventory information for each product. I'm trying to get the count of products purchased in a Table visualization with columns from the warehouse inventory table. I tried both of the measures below, but they both return the total Count for each row rather than sliced by product. Any help would be greatly appreciated! NumProductsfromSales1 = calculate([Count], treatas(values('Sales'[Product]), 'Inventory'[Product])) NumProductsfromSales2 = var lineage = treatas(values('Sales'[Product]), 'Inventory'[Product]) var tbl = calculatetable('Inventory Detail', KEEPFILTERS(lineage)) var result = calculate(sumx(tbl, [Count])) return result
From this source, we see TREATAS works as follows. [Filtered Measure] := CALCULATE ( <target_measure>, TREATAS ( VALUES ( <lookup_granularity_column> ), <target_granularity_column> ) ) is equivalent to [Filtered Measure] := CALCULATE ( <target_measure>, INTERSECT ( ALL ( <target_granularity_column> ), VALUES ( <lookup_granularity_column> ) ) ) The important part is the ALL function. That's why you're losing the filter context from the rows in the visual. I'm not sure if this is the most efficient solution, but I think if you add Inventory as a filter table to your first attempt, it should maintain the filter context on that table from the row in the visual. NumProductsfromSales1 = CALCULATE ( [Count], 'Inventory', TREATAS ( VALUES ( 'Sales'[Product] ), 'Inventory'[Product] ) ) Edit: Regarding your comment, try the following: a = VAR top5prod = SELECTCOLUMNS ( TOPN ( 5, SUMMARIZE ( Sales, Sales[Product], "Count", [Product Count] ), [Count] ), "Product", Sales[Product] ) RETURN CALCULATE ( [Product Count], FILTER ( 'Inventory', 'Inventory'[Product] IN top5prod ) ) Using FILTER isn't as efficient as TREATAS but see if it works. It's very difficult to answer this sort of question without having anything reproducible to work with.