I created a date column in the power BI using below query. But it gives me an error message as: The expression refers to multiple columns. Multiple columns cannot be converted to scalar value.
Date = ADDCOLUMNS (Calendar (Date (2021,01,01), Date (2030,12,30)),"DateAsInteger", FORMAT([DATE], "YYYYMMDD"), "Year", YEAR([Date]),
"YearMonthNumber", Format([Date],"YYYY/MM"), "MonthNameShort", FORMAT([Date],"mmm"),"MonthNameLong", FORMAT([Date],"mmmm"),
"Quarter" & FORMAT([Date],"Q"), "YearQuarter", FORMAT([Date], "YYYY" & "/Q" & FORMAT([Date],"Q")),
"Monthnumber", Format([Date],"MM"),"DayofWeekNumber", WEEKDAY([Date]), "DayofWeek", Format([Date],"dddd"), "DayofWeekShort", FORMAT([Date], "ddd"))
But I want the date data to be represented by year, month, quarter and all the means as per the query.
The DAX formula creates a Date table. Also, I fixed a minor issue in your formula.
Date = ADDCOLUMNS (Calendar (Date (2021,01,01), Date (2030,12,30)),"DateAsInteger", FORMAT([DATE], "YYYYMMDD"), "Year", YEAR([Date]),
"YearMonthNumber", Format([Date],"YYYY/MM"), "MonthNameShort", FORMAT([Date],"mmm"),"MonthNameLong", FORMAT([Date],"mmmm"),
"Quarter", FORMAT([Date],"Q"), "YearQuarter", FORMAT([Date], "YYYY" & "/Q" & FORMAT([Date],"Q")),
"Monthnumber", Format([Date],"MM"),"DayofWeekNumber", WEEKDAY([Date]), "DayofWeek", Format([Date],"dddd"), "DayofWeekShort", FORMAT([Date], "ddd"))
Related
I have a measure that sums expense amount for the current filtered year. This works as desired.
Expense = CALCULATE(SUM('GL Data'[Amount]),FILTER('GL Data','GL Data'[SubGrp1] = "EXPENSE"))
I want to sum the expense amount for the same time period of prior year. My measure calculates correct data as of 11 months ago, not 1 year ago. It appears to function as if the data calculation is looking at my system date, not the specified date:
Expense PY = CALCULATE([Expense], DATEADD(Dates[Date],-1,YEAR))
I've attempted using -12, -13 and MONTH in the DATEADD but the result doesn't correctly total 1 year ago. I've also tried SAMEPERIODLASTYEAR() which provides wrong info. Perhaps my Expense measure is the culprit.
My Date table is generated with this Dax:
Could you please use this as your date table, and test it. In your date table, you imposed no restrictions on the calendar auto() functions which I think is not useful. Also You used Generate function which ignores rows with blank values. In such a case, use GENERATEALL() function; but You need to form your date table like this:
Date =
VAR MinYear =
YEAR ( MIN ( Fact_Table[Date_Column] ) )
VAR MaxYear =
YEAR ( MAX ( Fact_Table[Date_Column] ) )
RETURN
ADDCOLUMNS (
FILTER (
CALENDARAUTO ( 12 ),
AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
),
"Calendar Year", "CY " & YEAR ( [Date] ),
"Day", FORMAT ( [Date], "dddd" ),
"Month Number", MONTH ( [Date] ),
"Month", FORMAT ( [Date], "mmmm" ),
"Year Month", FORMAT ( [Date], "mmm yy" ),
"Year", FORMAT ( [Date], "YYYY" )
)
After creating your Date Table, please do not forget to mark it as date table which is a good DAX practice as in the photo below.
I am new in Power BI, can anyone help me to convert the below query into DAX
Query:
select YEAR([CloseDate]),MONTH([CloseDate]), sum(days_to_close)/count(distinct id) as 'Number of Days to Close' from
(
select [CloseDate], id, days_to_close from [dbo].[test]
where sname='Closed' group by [CloseDate], id, days_to_close
) a
group by YEAR([CloseDate]),MONTH([CloseDate])
order by 1,2
MyCode:
CALCULATE(
DIVIDE(
SUM(test[days_to_close]),
DISTINCTCOUNT(test[id])
),
test[sname]="Closed",
)
For each ClientNo I want the Type classification corresponding to the last date of each Year:
Thus, the table above should be summarized as:
So, somehow, we need two intermediate tables:
Unique values for years, like VALUES(Table[Date].Year)
Unique values for ClientNo, like VALUES(Table[ClientNo])
Then for each combination of Year and ClientNo, get the latest date for each year and finally get the Type classification.
You should be able to do this in two steps along these lines:
Summary =
VAR MaxDates =
SUMMARIZE (
ADDCOLUMNS ( Table1, "Year", YEAR ( Table1[Date] ) ),
Table1[ClientNo],
[Year],
"MaxDate", MAX ( Table1[Date] )
)
RETURN
SELECTCOLUMNS (
MaxDates,
"ClientNo", [ClientNo],
"Year", [Year],
"Type", LOOKUPVALUE (
Table1[Type],
Table1[ClientNo], [ClientNo],
Table1[Date], [MaxDate]
)
)
In calculating the variable, we add a Year column and then calculate the maximal date corresponding to that year.
Then we take that table variable, pick out the ClientNo and Year columns, and look up what the Type corresponding to the MaxDate.
If Note: you want to keep the MaxDate column, replace
[...] SELECTCOLUMNS ( MaxDates, "ClientNo", [ClientNo], "Year", [Year], [...]
with
[...] ADDCOLUMNS ( MaxDates, [...]
I have a bunch of calculated DAX columns that I want to show as a visual. If I use a normal bar chart I get the following image, Barchart 1, where because I do not have any fields in the axis field. The titles of each of the calculated columns are what I want the x-axis to be similar to how it is in the funnel chart below.
The funnel chart only requires the value field to be filled in and it creates the following image which is kind of what I want but it needs to be vertical similar to the last image.
This final image, Barchart 3 is what I want to achieve with my calculated columns but so far I have had no luck in figuring this out. This visual was created using a different file which is irrelevant to the project I am working on now. I believe that if I could unpivot the calculated columns then it would create the graph I am looking for but I can't figure out how to unpivot columns that are created in DAX. Is there a way to unpivot DAX columns or a visual on the marketplace to accomplish what I am trying to do? Or would I need to create my own custom visual to accomplish this? Other ideas/thoughts?
Sample data file
I'd recommend creating a calculated table that has Month unpivoted so that you only need to put a single series on the bar chart.
For example, you can write a calculated table like this with only 7 columns:
CalcTable =
VAR ThisYear = YEAR ( MAX ( Sheet4[Start] ) )
RETURN
ADDCOLUMNS (
CROSSJOIN (
SELECTCOLUMNS (
Sheet4,
"Project", Sheet4[Project],
"Start", Sheet4[Start],
"End", Sheet4[End],
"Cost", Sheet4[Cost]
),
ADDCOLUMNS (
GENERATESERIES ( 1, 12 ),
"Month", FORMAT ( DATE ( ThisYear, [Value], 1 ), "MMMM YYYY" )
)
),
"MonthCost", IF (
[Value] >= MONTH ( [Start] ) && [Value] <= MONTH ( [End] ),
DIVIDE ( [Cost], DATEDIFF ( [Start], [End], MONTH ) + 1 ),
0
)
)
This table looks like this:
And allows you to create a bar chart with Month on the axis and sum of MonthCost for the values.
I ended up finding a solution to this. It doesn't maintain relationships but it works.
Totals Table =
UNION(
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[January 2020]),"Month","January 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[February 2020]), "Month", "February 2020"),
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[March 2020]),"Month","March 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[April 2020]), "Month", "April 2020"),
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[May 2020]),"Month","May 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[June 2020]), "Month", "June 2020"),
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[July 2020]),"Month","July 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[August 2020]), "Month", "August 2020"),
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[September 2020]),"Month","September 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[October 2020]), "Month", "October 2020"),
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[November 2020]),"Month","November 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[December 2020]), "Month", "December 2020"))
I am able to generate calendar table for each month, but it goes till the end of current month.
CalendarTest =
SUMMARIZE(
ADDCOLUMNS(
CALENDAR ("01-01-2018", TODAY()),
"Month", EOMONTH([Date], - 1) + 1
),
[Month],
"Eomonth",EOMONTH([Month],0)
But the last value for Eomonth should be today's date. Not the end of the month.
Need it to be like that:
Try
CalendarTest =
SUMMARIZE(
ADDCOLUMNS(
CALENDAR ("01-01-2018", TODAY() ),
"Month", EOMONTH ( [Date], - 1) + 1
),
[Month],
"Eomonth", MIN ( EOMONTH([Month],0), TODAY() )
)