Power-BI: Add column in Data-Area or Add Column in Power-Query - powerbi

I am wondering where the difference in both approaches lie.
Basically I want to add a column which indicates as a result with TRUE or FALSE if a data row is inside my time period I have to consider (all values older than current calender week - 1).
For my understanding I have two options:
Option 1:
I modify my data query and add a new column with a formula like this in Power-Query:
DATEDIFF(WEEKNUM([created].[Date]),WEEKNUM(TODAY()),WEEK)
Option 2:
I use the Data-Section in Power-BI and add a column wiht this formula:
DATEDIFF(WEEKNUM([created].[Date]),WEEKNUM(TODAY()),WEEK)
What is the difference of both approaches, using either the backend PowerQuery vs the Data-Section in Power-BI. Is one more favourable?

Common answer in PowerPivot world suggests calculated columns are very costly, therefore better to choose PowerQuery to do the data preparation work. I suppose it depends on how many rows you have and how many other calculations you are asking PP/DAX to do as you are now storing a value that can be easily calculated.
Independent of "cost", I tend to use DAX for dynamic calculations and land more static values using PQ/M which allows for some very creative extract/transform/load (ETL). Think of it this way: if you put the formula in Excel, calculate for 1MM rows it has to recalculate every time you do, well, anything. So what do you do? Use the formula to calc the value then paste values so you just keep the answer. PQ can deliver the final result and drop the calcs or -- better -- intermediate data.
JR

Related

Power BI DAX - Measure to change name of row value

I need some help creating a measure to change the name of "FROM_USER" in the slicer here.
I think I need to use the function SELECTEDVALUE, but I have not managed to get it working.
So, the column has only two values, CRAWLER and FROM_USER.
Any suggestions would be helpful!
See picture
Measures can't be used as slicer values. If you want the column values to be changed and yet to be used in a slicer, you need to create a calculate column to change that.
Column = IF('Table'[Column1]="FROM_USER","desiredValue","CRAWLER")
If you are really keen on using a measure to slice, you need to build a disconnected table and follow the method described here. But the performance will take a hit depending on how complex your data model and calculations are.

Calculate % of two columns Power BI

I want to calculate % of two columns which are already in %.
I want to calculate formula like
Target achieved= ACTUAL/TARGET
But here is ACTUAL is already a measure/calculated metrics so I'm not able to divide these two columns.
Any help would be appreciated..
Make sure both target and actual are actually numbers and not strings. You can do it in transform data (aka Power Query) part, before data is loaded into the report.
After that you should be able to create the measure you want, e.g. something like that:
UPDATE : What happens if Actual is not a column, but a measure?
If Actual measure is based on the columns in the same table as target you shouldn't have a problem. You cannot combine measure and column in the same formula. Measure is an aggregated field and needs to be grouped by another field (if you are familiar with SQL,think of SUM and GROUP BY). Coming back to your problem, you need to create measure out of "Target" column as well (notice I have in the formula SUM('Table'[Plan]) which makes it a measure). Than you can use both of them in the formula, but of course you need to "group" them by something(e.g. date) otherwise it will just show you a total.

How to use Measures in Calculated column Formula in PBI

I'm trying to use a previous calculated Measure in a Calculated Column in PBI, but i get the error "expressions that yield variant data-type cannot be used to define calculated columns", how can i fix this? this is my code
Critical_Detrended = 1*(1+[Trend])
Thanks
Calculated columns are static. They are calculated once, when the data is loaded. Their values cannot change. Thus, you can't use something dynamic, like measures, because measures are responsive, they are re-computed when the user selects something, changes the value of a slicer, etc.
The solution in your case is to change your calculated column to be a measure too.
You could take a look for example at Calculated columns and measures in DAX article.
While calculated columns are not responsive to any slicers or filters set on your report, you absolutely can use measures in a calculated column definition so long as you understand how the row context to filter context transition works and don't expect the calculated column to be dynamically responsive.
That said, the error you are getting sounds like [Trend] may output different data types in different situations but a column can only be a single data type. A non-obvious case where this sort of thing happens is when a measure sometimes outputs an integer and other times outputs a decimal type. In a case like that, you can force it to return a decimal type by multiplying by 1.0 or adding 0.0 or similar manner of type coercion. In your case, changing one of the 1 to 1.0 might do the trick if that's indeed the problem.

Power BI conditional formatting based on percentiles

Is there a way in Power BI to apply conditional formatting based on percentiles? It is very useful feature if the data contains outlier observations.
In Excel I have been used to easily make it:
I know I can make formatting based on whatever field which can be a measure.
I wanted to construct a DAX measure:
Sales_per_category_percentile = PERCENTILE.EXC(DISTINCT(table[Category]) , [Sales]))
But it does not accept DISTINCT(table[Category]) as the first argument of the PERCENTILE.EXC function.
You should be able to do conditional formatting on a Matrix.
Just got released on the power-bi-desktop-july-2019
In addition to adding a new type of conditional formatting, we’ve
extended in general our Rules form of conditional formatting to
support Percent, which allows you to apply formatting on a dynamic
range of data, instead of having to specify an absolute number that
can become absolute as data refreshes. We also added the Reverse
button from the Icon dialog for colors as well, so it’s easier to
quick swap the direction of your rules.
Let's look at the PERCENTILE.EXC definition.
PERCENTILE.EXC(<column>, <k>)
Term Definition
column A column containing the values that define relative standing.
k The percentile value in the range 0..1, exclusive.
EDIT:(you are correct VALUES does not work)
You have to supply a column. And choose your K between 0 and 1 exclusive, let's say 0.5
Sales_per_category_percentile = PERCENTILE.EXC('Fact'[Salesamount],0.5)
And use it in a Matrix with your table[Category] in Rows for example.
EDIT2: For binning and grouping have a look at:
Grouping and Binning in Power BI

How to build a matrix, similar to the table from Google analytics

In PowerBI I'd like to build Non-standard matrix very similar to the report in Google Analytics.
What do I have now:
I want to change my subtotal to measure, which is calculated as the difference in percentage of the two values
What I want to get:
In Power BI, there is no way to override the subtotals of a matrix with a calculation. Part of the challenge is that you know there are only two date ranges, but as far as Power BI is concerned, there could be any number of date ranges.
It's difficult to tell from your question exactly what input you have and what output you're looking for. Further, the numbers in your screenshots are obscured. However, one consideration would be to solve the problem using measures (i.e. a measure representing the first date range, a measure representing the 2nd date range, and then a measure calculating the difference between them). You may need to change the layout of your visual a little to make this work and the specific design would depend on how static your date ranges are.