"Expression.Error: A cyclic reference was encountered during evaluation" - powerbi

Got this error after I have written a m code in power bi i.e 'Table.AddColumn(#"Inserted Month Name", "Custom", each if [Fiscal Year]= List.Max(#"dim_date"[Fiscal Year]) then "Y" else "N")'. Can anyone help me solve this problem?

Related

How to calculate average of a measure using Dax in power BI

In power bi, I created a measure to calculate the daily average of each department head. I got an error. First I used this formula. I got an error message like
'column is not found in query1'
.
AvgBydepartmentHead =
DIVIDE(
SUM(TableName[Total]),
SUM(TableName[Days worked])
Then I used the other formula
AvgBydepartmentHead =
DIVIDE(
SUM([Total]),
SUM([Days worked])
Got error like
The sum function only accepts column reference as the argument 1
Both Total and days worked are measures.
[1]: https://i.stack.imgur.com/1kv5b.png
How to resolve this issue. Thanks
Not sure what you mean with "calculated fields", but if they are Measures you don't need an additional aggregation, the following should work:
AvgBydepartmentHead = DIVIDE( [Total], [Days worked] )
It's also unclear what the message 'table is not found in query1' has to do with your problem.

How to calculate average weekly active visitor using DAX in PowerBI

*Take note that Visit 1 means the first visit of the day while Visit 2 means the 2nd visit for the day.
Hello everyone,
I'm trying to calculate average weekly active visitors in PowerBI using DAX.
First, I need to calculate how many unique visitors visit the shop in one week (Mon-Sun). Based on the calendar, 5/10/2022 - 5/13/2022 will be categorized under one week, and 5/16/2022 - 5/18/2022 is another week. So for the week from 5/10/2022 - 5/13/2022, there are 9 unique visitor and for the week from 5/16/2022 - 5/18/2022, there are 3 unique visitors.
Once I found out the unique visitor for respective weeks, then I can get the average weekly active visitor by:
(9+3)/2 = 6 visitors
Hence, the answer for the example in the screenshot will be 6 visitors. The output will be used in card visualization.
Struggling to find the best way to do this, any help or advise will be greatly appreciated!
Sample Data:
https://docs.google.com/spreadsheets/d/10TsJUy-Lkdpb9Eeh5itx-XE59hytC_i5I_6UeLLHS84/edit#gid=0
I don't know enough about DAX (or Power BI) to devise a DAX only solution.
However, you can calculate the Distinct Visitors per week using Power Query M Code (Home=>Transform in Power BI), and then create a Measure to show the Average visitors per week on the card.
Power Query M Code
Edit: Change from computing weeknumber to computing start of week to avoid problems if dates span more than a year, per comment below by #weizer
let
//change next line to whatever your actual source is
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//Don't need this column for the output
#"Removed Columns" = Table.RemoveColumns(Source,{"Visit"}),
//Set data types
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Visitor ID", Int64.Type}, {"Date", type date}}),
//add custom column referenced to "StartOfWeek" so we can group by week
#"Added Custom" = Table.AddColumn(#"Changed Type", "WeekNumber", each Date.StartOfWeek([Date],Day.Monday), type date),
//Group by week number
// Then aggregate by week range and distinct visitor count
#"Grouped Rows" = Table.Group(#"Added Custom", {"WeekNumber"}, {
{"Date Range", each Date.ToText(List.Min([Date])) & " - " & Date.ToText(List.Max([Date])), type text},
{"Distinct Visitors", each List.Count(List.Distinct([Visitor ID])), Int64.Type}
}),
//Remove unneeded Weeknumber column
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"WeekNumber"})
in
#"Removed Columns1"
The Measure for the card will be just a simple Average function:
Distinct Visitors per Week = Average(yourTableName[Distinct Visitors])
VAR days =
SUMMARIZE(
'Sheet1'
,'Sheet1'[Date]
)
VAR Calend=
ADDCOLUMNS(
days
,"week",WEEKNUM('Sheet1'[Date])
)
VAR weeks=
SUMMARIZE(
Calend
,[week]
,"qty",COUNTROWS(VALUES('Sheet1'[Visitor ID]))
)
RETURN
AVERAGEX(
weeks
,[qty]
)

Change the code below from DAX to Power Query

Can someone help me to transfer this code to Power Query. I found this one in dax, but I would prefer to have it done already in the data transformation.
Avg Activation Time =
var Oppy_id = 'Calculation'[Reference number]
Return
AVERAGEX(
FILTER(ALL('Calculation'), 'Calculation'[Reference number] = Oppy_id),
'Calculation'[Activation (wd)])
Without examples of your data and desired output, it is not possible to be more specific, but this should get you started.
In PQ to average values that are linked to a particular group (ID), you would GroupBy the ID, and select Average for the aggregation (with Activation Days for the Value column.
In M, it would look generally like:
#"Grouped Rows" = Table.Group(#"Previous Step", {"ID"}, {
{"Average Activation Days", each List.Average([Activation Days]), type nullable number}
}),
In your code, the step would have different references for the previous step, and possibly for the column.

Power BI date range custom column for filter

I have a calendar table in Power BI linked to two other tables, one with occupancy by date and another with predicted occupancy by date. The second table goes well into the future.
I want the report to have a rolling 15 day range, 7 days prior to today and 7 days into the future. I tried to create a custom column using:
ReportRange = IF(DATESBETWEEN (Calendar[SQL_Date], (TODAY()-7), (TODAY()+7)),1,0)
I get a response "No syntax errors have been detected."
But when I click "OK", I get a yellow bar/warning:
"Expression.Error: The name 'IF' wasn't recognized. Make sure it's spelled correctly."
Can anyone help with this?
Thanks!
You need to write custom columns in the query editor in M code, not DAX.
Something like this may work:
if Date.IsInPreviousNDays([SQL_Date], 7) and Date.IsInNextNDays([SQL_Date], 7)
then 1
else 0
You may prefer to use relative date filtering instead though.
You are attempting to use a PowerQuery M Language column through the custom column editor. This will not work in M. You will need to create a measure in DAX to do the calculation.
In your DAX you can use DatesBetween, as per this example:
=CALCULATE(SUM(InternetSales_USD[SalesAmount_USD]), DATESBETWEEN(DateTime[DateKey],
DATE(2007,6,1),
DATE(2007,8,31)
))
You can use the TODAY keyword to work calculations off the current date.
for "rolling" filters i found it very useful to build generic offset columns for day/week/month/quarter into your date table. with that you can easy filter a visual e.g. with "week offset" > 1 and "week offset" <-1 to get rolling 2 weeks views... Detailed howto can be find here: https://radacad.com/offset-columns-for-the-date-table-flexibility-in-relative-date-filtering-for-power-bi
If you just want to filter the data coming in to the report, you can use a filter.
You can do this in the Advanced Editor, sample below.
let
Source = BillingData,
#"Removed Other Columns" = Table.SelectColumns(Source,{"Date"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Date", type date}}),
#"Removed Duplicates" = Table.Distinct(#"Changed Type"),
#"Filtered Rows" = Table.SelectRows(#"Removed Duplicates", each Date.IsInPreviousNDays(Date.AddDays(DateTime.LocalNow(), 7), 14))
in
#"Filtered Rows"
I started with the standard date filter and customised it using the advanced editor to add 7 days and then subtract 14.

Months To date formula not working properly on Power Bi

I am newbie in Power Bi. I am calculating months to date of a measure.
I have written following DAX formula for that,
MTD in Sales = CALCULATE([Total Sales], DATESMTD(Dates[Date]) )
it shows me correct total sales value of this month.But when i make day-wise, it shows me some unrealistic value.
I have attached a screenshots of my result..Please have a look.
I don't understand what wrong are going on? Can you please find out the problem plz?
DATESMTD(Dates[Date]) is equivalent to:
CALCULATETABLE(
FILTER(
ALL(Dates[Date]),
AND(
Dates[Date] <= MAX(Dates[Date]),
AND (
YEAR(Dates[Date]) = YEAR(MAX(Dates[Date])),
MONTH(Dates[Date]) = MONTH(MAX(Dates[Date]))
)
)
)
)
This only considers the maximum value of the dates in the external filter context, so for Tuesday (today), it will contain every day of the month up to today, for Monday (yesterday) it will contain every day of the month up to yesterday and so on. (Assuming that no Sales are linked to future dates).
If you want to further filter this to only include sales that occurred on the given day of the week, I'd suggest altering MTD in Sales to:
[MTD in Sales] := CALCULATE([Total Sales], DATESMTD(Dates[Date]), Dates[DayOfWeekName])
This will additionally filter the included dates to only those with DayOfWeekName values present in the external filter context.