SQL TO DAX Conversion - powerbi

New to Power BI and needing some help on a Dax Conversion. The Date 20191130 is being passed in via a Slicer from another page So I am not sure how to Set up my dax in a such a way to read the following below:
Current Value
SELECT SUM(value) FROM Table
Where DateKey BETWEEN 20181201 AND 20191130
Previous Value
SELECT SUM(value) FROM Table
Where DateKey BETWEEN 20171201 AND 20181130
(Current Value - Previous Value)/(Previous Value)

Assuming Slicer Date is in Date format, you will have to create two different calculations:
Current Value:
Current Value =
VAR End1 = Table[SlicerDate]
VAR Start1 = DATE(YEAR(End1)-1,MONTH(End1),DAY(End1))+1
VAR End2 = DATE(YEAR(End1)-1,MONTH(End1),DAY(End1))
VAR Start2 = DATE(YEAR(End1)-2,MONTH(End1),DAY(End1))+1
RETURN CALCULATE(COUNT('Table'[Name]),'Table'[Datekey]>=Start1 && 'Table'[Datekey]<=End1)
Previous Value:
Previous Value =
VAR End1 = Table[SlicerDate]
VAR Start1 = DATE(YEAR(End1)-1,MONTH(End1),DAY(End1))+1
VAR End2 = DATE(YEAR(End1)-1,MONTH(End1),DAY(End1))
VAR Start2 = DATE(YEAR(End1)-2,MONTH(End1),DAY(End1))+1
RETURN CALCULATE(COUNT('Table'[Name]),'Table'[Datekey]>=Start1 && 'Table'[Datekey]<=End1)
If your slicer date is in numeric or text format:
Current Value:
Current Value =
VAR End1 = Date(Left(Table[SlicerDate],4),Right(left(Table[SlicerDate],6),2),right(Table[SlicerDate],2)
VAR Start1 = DATE(YEAR(End1)-1,MONTH(End1),DAY(End1))+1
VAR End2 = DATE(YEAR(End1)-1,MONTH(End1),DAY(End1))
VAR Start2 = DATE(YEAR(End1)-2,MONTH(End1),DAY(End1))+1
RETURN CALCULATE(COUNT('Table'[Name]),'Table'[Datekey]>=Start1 && 'Table'[Datekey]<=End1)
Previous Value:
Previous Value =
VAR End1 = Date(Left(Table[SlicerDate],4),Right(left(Table[SlicerDate],6),2),right(Table[SlicerDate],2)
VAR Start1 = DATE(YEAR(End1)-1,MONTH(End1),DAY(End1))+1
VAR End2 = DATE(YEAR(End1)-1,MONTH(End1),DAY(End1))
VAR Start2 = DATE(YEAR(End1)-2,MONTH(End1),DAY(End1))+1
RETURN CALCULATE(COUNT('Table'[Name]),'Table'[Datekey]>=Start1 && 'Table'[Datekey]<=End1)
Hope this helps.

Related

How to display total correct in measure

I'm trying to compare difference between years in sales but I'm having the following issue:
I have this:
Valor Actual = -CALCULATE(SUM(Apuntes_Resultado[Total Valor]), Apuntes_Resultado[IDEscenario]=1)
Total Valor Previo =
VAR SubgrupoFilter = ISFILTERED(TCuentas[SubGrupo])
VAR TipoCuentaFilter = ISFILTERED(TTipoCuenta[IDTipoCuenta])
VAR Variablefilter = OR(SubgrupoFilter,TipoCuentaFilter)
VAR Resultado = IF(Variablefilter, [Valor Actual], [ActualAjust])
RETURN
Resultado
I use the above code to have this measure
`
Total Valor Final =
VAR IDVistaDetalle = SELECTEDVALUE(TTipoCuenta[Vista Detalle])
VAR IDDetalle = SELECTEDVALUE(TTipoCuenta[Detalle])
VAR IDDetalleVisible = ISFILTERED(TCuentas[SubGrupo])
VAR Resultado = SWITCH(TRUE(),IDDetalleVisible=TRUE() && IDDetalle = 0, BLANK(),
IDVistaDetalle = 1, [Total Valor Previo],
IDVistaDetalle = 2, [Valor Acumulado])
RETURN Resultado`
this works properly. But i'm trying to normalize this values with laboral day's between years.
To this I have created the following column in my date table:
`Laboral Day =
VAR EsFinDeSemana = Dates[Number Day] IN {7}
VAR EsFestivo =
RELATED(TablaFestivos[Fecha])
RETURN
IF (EsFestivo || EsFinDeSemana,0,1)`
It works fine. Shows properly laboral days and holidays
After that, I'm using the follow measure to calculate the values adjusted by year
`AdjustYear = var total =CALCULATE([Total Valor Final], SAMEPERIODLASTYEAR(Dates[Date]))
var LBCY= CALCULATE(SUMX(Dates,Dates[Laboral Day]))
var PYLB= CALCULATE(SUMX(Dates,Dates[Laboral Day]),SAMEPERIODLASTYEAR(Dates[Date]))
return - DIVIDE(total, PYLB)*LBCY //value from previousyear/PYlaboraldays * CYlaboraldays
`
This is a sample of the result:
as you can see, it is taking the same total for two columns but values are different in rows.....
Totals for 2022 and 2021 are ok, laboral days are okey but B 2021 ajus is taking the same total of A 2021
I'm using my date table to filter by months.
Any help?
myMeasure=
VAR myTbl =
Addcolumns(
Summarize(
Dates
,Dates[yearMonth]
)
,"total",[AdjustYear]
)
RETURN
SUMX(myTbl ,[total])
OR
myMeasure=
VAR myTbl =
Addcolumns(
VALUES(Dates[yearMonth])
,"total",[AdjustYear]
)
RETURN
SUMX(myTbl ,[total])
OR
myMeasure=
VAR myTbl =
Addcolumns(
VALUES(Dates[Month]) -- the same column that you use in the matrix
,"total",[AdjustYear]
)
RETURN
SUMX(myTbl ,[total])

DAX Power BI - addcolumns to calculated table

I have a task to compare 2 dynamic periods from the table (MOO).
Th idea is to get clients which are in both dates and compare them by 1 field (Rating_rank).
But my created field calculates max from all table, not grouping by client.
What should id do?
rate_worse_tab =
var min_dt = calculate(min('MOO'[value_day]),ALLSELECTED('MOO'[value_day]))
var max_dt = calculate(max('MOO'[value_day]),ALLSELECTED('moo'[value_day]))
var cur_cl = CALCULATETABLE(values(MOO[CLIENT_UK]),filter(MOO,MOO[VALUE_DAY]=max_dt))
var old_cl = CALCULATETABLE(values(MOO[CLIENT_UK]),filter(MOO,MOO[VALUE_DAY]=min_dt))
var combo_table = CALCULATETABLE(values(MOO[CLIENT_UK]),filter(MOO, MOO[CLIENT_UK] in cur_cl && MOO[CLIENT_UK] in old_cl))
var f_table = ADDCOLUMNS(combo_table,"Old_rate_rank",calculate(max(MOO[Rating_rank]),filter(MOO,MOO[VALUE_DAY]=min_dt && MOO[CLIENT_UK] in combo_table)))
return
f_table

How to create a new column based on previous row with DAX - PowerBI

Hi on powerBI I have something like this
Desire result is this to create another column 'is_repeat from previous row'
where it is 'True' when the current row is repeat the top row
I tried to use 'EARLIER' function to compare current row to previous but EARLIER grey out my 'index' column - not sure why
thanks in advance!
Calculated Column:
=
VAR ThisIndex = Table1[INDEX]
VAR PreviousIndex = ThisIndex - 1
VAR ThisCode = Table1[code]
VAR PreviousCode =
LOOKUPVALUE( Table1[code], Table1[INDEX], PreviousIndex )
RETURN
ThisCode = PreviousCode

How to calculate Dynamic Moving Average without any date reference?

I am looking for some clarification with regards to moving average calculation. My data looks like the screenshot attached.
I checked online for suggestion but it went awry. Most of the moving averages work with date (in date format). As I am having Day...only in numeric format...I am not sure which function would help me to get Mov_Avg for 3 Day time frame on the Increment col. My desired output should be col 3_day_Avg
You can use your column Increase (created using Power Query) and create this below measure for your required output-
last_three_day_average =
VAR current_row_day = MIN(your_source_table_name[day])
VAR current_row_country = MIN(your_source_table_name[country])
VAR current_row_case_death = MIN(your_source_table_name[cases/death])
VAR current_row_count = MIN(your_source_table_name[count])
RETURN
CALCULATE(
AVERAGE(your_source_table_name[increase]),
FILTER(
ALL(your_source_table_name),
your_source_table_name[country] = current_row_country
&& your_source_table_name[cases/death] = current_row_case_death
&& your_source_table_name[day] <= current_row_day
&& your_source_table_name[day] >= current_row_day - 2
)
)
Here is the final output-
Alternatively, if you wants not to use Power Query, You can use Custom column and that case you have to convert your Measure increase_using_measure as a column. As aggregation is required, you can not refer a measure for that. Here below is the code for column-
increase_using_column =
VAR current_row_day = your_source_table_name[day]
VAR current_row_country = your_source_table_name[country]
VAR current_row_case_death = your_source_table_name[cases/death]
VAR current_row_count = your_source_table_name[count]
VAR previous_day_count =
LOOKUPVALUE(
your_source_table_name[count],
your_source_table_name[day],current_row_day-1,
your_source_table_name[country],current_row_country,
your_source_table_name[cases/death],current_row_case_death
)
RETURN if(previous_day_count = BLANK(),current_row_count, current_row_count - previous_day_count)
Now you can use the above column created using DAX in your Last 3 days average calculation as below-
last_three_day_average =
VAR current_row_day = MIN(your_source_table_name[day])
VAR current_row_country = MIN(your_source_table_name[country])
VAR current_row_case_death = MIN(your_source_table_name[cases/death])
VAR current_row_count = MIN(your_source_table_name[count])
RETURN
CALCULATE(
AVERAGE(your_source_table_name[increase_using_column]),
FILTER(
ALL(your_source_table_name),
your_source_table_name[country] = current_row_country
&& your_source_table_name[cases/death] = current_row_case_death
&& your_source_table_name[day] <= current_row_day
&& your_source_table_name[day] >= current_row_day - 2
)
)
Output will be same as shown above.

Apex Interactive Grid how to retrieve a specific record

I am retrieving my grid data using:
var ig$ = apex.region("myGrid1").widget(),
view = ig$.interactiveGrid("getCurrentView");
Now I want to check for a specific record based on 2 columns: id1 and id2 where id1 = 1 and id2 = 7
How can I do that with javascript?
You can iterate for each record like this:
//"myGrid1" should be the static id of the IG region
var widget = apex.region('myGrid1').widget();
var grid = widget.interactiveGrid('getViews','grid');
var model = grid.model;
var results = [];
model.forEach(function(r) {
var record = r;
//the name of the columns should be ID1 and ID2, if not
//make the necessary changes using "_" to represent "space"
var value1 = model.getValue(record,'ID1');
var value2 = model.getValue(record,'ID2');
if(value1 == 1 && value2 == 7) {
results.push(record);
}
})
console.log(results);
To test this code, execute it on console.
To start the console on chrome just press F12
good luck.