Value from next row - DAX - date

I'm trying to create following calculated column in Power BI data model:
Logic:
Sort dataset based on ID & Start Date and show value from column Status from next row.
(1 ID can have one or more SubIDs)

This will work for a calculated column:
Expected Result =
VAR _start_date = [Start Date]
VAR _id = [ID]
VAR _t = FILTER(TableName, [ID] = _id && [Start Date] > _start_date)
VAR _next_date = MINX(_t, [Start Date])
RETURN MINX(FILTER(_t, [Start Date] = _next_date), [Status])

Related

Display Data between Dec Y-1 and Dec current Y in PowerBI

I am new in powerBI, I loonking to display data in during a closing period and in my case it is from :
31/12/Y-1 to 31/12/Y my issue it is with slicer the year filter all date in the current year and not taking into account the value in 31/12/Y-1
How can I set PowerBI to do so,
thanks for your support
Display Sum of data in a defined period 31/12/Y-1 till 31/12/Y, user can select the period that they want to display and the data will update via PowerBI
Slicer below :
Use this calculated column
Closing Period =
VAR thisYear = YEAR(TODAY())
VAR startDate = DATE(thisYear -1 , 12, 31)
VAR endDate = DATE(thisYear, 12, 31)
RETURN
IF(startDate <= 'Date'[Date] && 'Date'[Date] <= endDate, thisYear)
and filter the 'Date'[Closing Period] column on the current year.
You can use a new calculated column for the slicer in your case:
Filter_date = DATEADD ( 'Date Table'[date].[Date], +1, DAY )

Auto-populate today's date if no date entered

I currently have a start date in cell C1 and end date in C2 of a Google Sheet spreadsheet. I would like to set it up so that if no date is entered in the end date (C2), this cell will be auto-populated with today's date
I have thus far found the following script
function onFormSubmit(e) {
//edit responses sheet name
var responseSheetName = 'Stats';
//Edit colmn number, column in which the date has to be autopopulated
var column = 3;
//Get target row number
var row = e.range.rowStart;
//If no date, pouplate the cell with current date
if(!e.values[column-1]){
SpreadsheetApp.getActive().getSheetByName(responseSheetName).getRange(row, column).setValue(new Date())
}
}
This doesn't seem to be doing the trick so either I am reading it wrong, it is not what I am looking for!
Is this something that is possible?

Second minimum date in power bi

I have some users with different game_id.
for each user, I want to find the second minimum date. (column: min2_date)
If a user doesn't have a second date (look at user_id: 2, in this example), his min2_date should be -1.
If the second minimum date is the same as the first minimum date(look at user_id: 4), we should write that date in the min2_date column.
I don't know How I should calculate the second minimum date in Power BI.
please help me if you know.
Because you dont want a real second min date (but date in second row by order), we must try some trickyway.
One of way that we can do that is:
MinDate2 =
var _countrows = CALCULATE(countrows(VALUES(games[dates])), ALL(games[dates]) )
return
if(_countrows = 1, -1,
FORMAT(DISTINCT(TOPN(1,TOPN(2,CALCULATETABLE(SELECTCOLUMNS(games, "dates",games[dates]), ALL(games[dates])),[dates], asc), [dates], desc)), "yyyy-mm-dd")
)
where MinDate and MinDate2 are measures.
AS a calculatedColumn:
MinDate2_col =
var _countrows = CALCULATE(countrows(VALUES(games[dates])), ALL(games[dates]) )
return
if(_countrows = 1, "-1",
FORMAT(DISTINCT(TOPN(1,TOPN(2,CALCULATETABLE(SELECTCOLUMNS(games, "dates",games[dates]), ALL(games[dates])),[dates], asc), [dates], desc)), "yyyy-mm-dd")
)

Rolling average a certain period using a DAX measure in Power BI

I tried generating a DAX measure as a rolling average. I don't quite know how to insert the measure I need rolling averaged as "CPI_annualized". It is not giving an option to bring CPI_annualized into the Rolling Average calc.
This is the error I am given when trying to construct the RollingAverage VAR in the measure P_MA
The syntax for 'CALCULATE' is incorrect. DAX(VAR __LAST_DATE = LASTDATE('public econometrics'[date]) ......
This is the DAX measure that I am trying to complete :
P_MA =
VAR __NUM_PERIODS = 3
VAR __LAST_DATE = LASTDATE('public econometrics'[date])
VAR RollingAverage =
AVERAGEX(
DATESBETWEEN(
'public econometrics'[date],
DATEADD(__LAST_DATE, -__NUM_PERIODS, MONTH),
__LAST_DATE)
CALCULATE([CPILFESL]))
)
RETURN RollingAverage
This is the DAX measure that I am trying to use in the rolling calculation with the data in my dB given monthly. This works as below.
CPI_annualized = (CALCULATE(SUM('public econometrics'[value]),'public
econometrics'[econometric_name]=="CPILFESL")/CALCULATE(SUM('public
econometrics'[value]),'public
econometrics'[econometric_name]=="CPILFESL",SAMEPERIODLASTYEAR('public econometrics'[date])))-1
Inserting this measure in a line chart gives my this table.
date
CPILFESL
1 January, 1978
6.41%
1 February, 1978
6.20%
I think you are getting error message due to this line:
CALCULATE([CPILFESL])
In Dax Calculate measure, you cannot directly calculate a value, instead you have to include a number calculation function such as sum, max, min, here is the example:
CALCULATE(sum([CPILFESL]))
In addition, you can get rid of Calculate measure when there is no filter expression, such as sum([CPILFESL]) is enough.
A proper way of using calculate is to filter the value, as shown in online documentation
CALCULATE(
SUM(Sales[Sales Amount]),
'Product'[Color] = "Blue"
)
You can find many usefull templated here:
https://www.daxpatterns.com/month-related-calculations/
Moving average 3 months
VAR MonthsInRange = 3
VAR LastMonthRange =
MAX ( 'Date'[Year Month Number] )
VAR FirstMonthRange =
LastMonthRange - MonthsInRange + 1
VAR Period3M =
FILTER (
ALL ( 'Date'[Year Month Number] ),
'Date'[Year Month Number] >= FirstMonthRange
&& 'Date'[Year Month Number] <= LastMonthRange
)
VAR Result =
IF (
COUNTROWS ( Period3M ) >= MonthsInRange,
CALCULATE (
AVERAGEX ( Period3M, [Sales Amount] ),
REMOVEFILTERS ( 'Date' )
)
)
RETURN
Result

Date query with the current date between two date_time columns

I have a fusion table with two date_time columns. The fist one is the start date (Startdatum) and in the other column is the end date (Einddatum).
I want to do a query with the current date, and only show the KML-lines on a map where the current date lies between the start and end date.
I tried to use the code below to create a string with a date format:
var time_date = new Date();
var day = time_date.getDate();
var month = time_date.getMonth()+1;
var year = time_date.getFullYear();
var date = (year+"."+month+"."+day);
To show the KML-lines on the map I tried to use the following code:
layer = new google.maps.FusionTablesLayer({
map: map,
heatmap: { enabled: false },
query: {
select: "col2",
from: "1mOMP1seJq4FdiNTugsfylZaJc8sKcSlfJKUuTJjv",
where: "'Startdatum' <= date AND 'Einddatum' >= date"
},
options: {
styleId: 2,
templateId: 2
}
});
Unfortunatly the map shows all the KMS-lines regardless what date is in one of the columns.
What am I doing wrong?
the where-clause is wrong, it has to be
where: "Startdatum <= '"+date+"' AND Einddatum >= '"+date+"'"
the date-format seems to be wrong. Although the used format yyyy.MM.dd is defined in the documentation, it doesn't work. The format yyyy-MM-dd currently works for me(but it's not defined in the documentation).
var date = (year+"-"+month+"-"+day);
(in case that day and month be less than 10 they wouldn't match the pattern, but that doesn't seem to be an issue)
Beyond that: when you fix these 2 mentioned parts it currently works(for me), but I've tried it a couple of hours ago and got unstable results.