Calculate Value For Dates Between - dax

A few references:
Microsoft's documentation on DATESBETWEEN.
Somewhat similar question, though the answer and derivatives of the formula don't return the correct results.
Microsoft's documentation on TODAY
Per the above Microsoft documentation, I'm trying to get a calculation for the last three months based on today's current date in SSAS Tabular model. First, I have no idea how to use SSAS and my company doesn't provide any learning material, so I've been reading through the MSDN documentation, which may not be the place to start, so if this is wrong, I'd appreciate being told so. For instance, with C# or Ruby, I can test code in a console to see if I get the result that I want, and I don't see how I can do that in SSAS Data Tools' DAX language - this is a GUI which gives users very little power over what they can do (it took me four hours to figure out how to access a dimension's properties). I am definitely a code monkey.
I tried using the below formula (and derivatives of it) because this is what it looks like Microsoft is doing in their example:
3MonthValue:=CALCULATE(SUM([MeasureOne])/SUM([MeasureTwo]),DATESBETWEEN(DateDimension[Date],DATEADD(DateDimension[Date],-3,MONTH),TODAY()))
The result, nothing. Of course, if I run similar SQL logic, I get the right results. I also used the provided SO example, though I suspect that's not exactly what I'm trying to achieve, and only obtained blanks as answers. Given that I need to calculate a formula between a certain time frame, which in SQL would be the WHERE clause, how do I translate this into DAX? In other words, what is DAX's WHERE and if CALCULATE isn't right, what's the correct approach?

When you say it isn't working, how do you mean? The formula you are using refers to your date dimension's key as the starting date for your DATESBETWEEN function - this means if you are expecting the measure to populate a value, you'll need to be using a particular date in your pivot to establish context.
If you are trying to view the measure at design time, in the editor, there is no context so the measure wont populate.
Moreover, if in a pivot you're looking at a time context that includes more than one date, that also will not work. So say you are looking at a month, or a quarter. Both of these encompass what amount to multiple DateDimension[Date]'s - so again context cannot be established.
so to recap - measures which look at date ranges like DATESBETWEEN using a starting time context that is set to your dimensions time key will only show up in a pivot when the pivots data is filtered to a single date.
You can test this using the same function, but hard set the starting date by replacing DateDimension[Date] with a static date (or possibly TODAY()). The measure should show up in design time because the formula has all the information it needs to complete the calculation.

Related

DAX ignore row context in measure. Calculate for a defined set of dates and show the value for all dates in visual

I am trying to create a measure which calculates the average daily revenue per customer, but only using days in a 6 months period prior to a specific date (where some type of conversion happens).
This specific date can be different for each customer.
The intention is to use the measure as a baseline for indexing daily average in the days/months/years after said conversion date.
If I put my current version of the measure in a card it works just fine (circled in green). But I will eventually have to visualize this over time as well. Thus I need the value to stay the same regardless of the row/date context in a table or timeline (circled in orange).
I suspect I need to use one of the ALL/ALLSELECTED/ALLEXCEPT filter modifiers but I can't really get anything to work.
The measure looks like this for now:
Average daily rev before conversion = CALCULATE (
AVERAGEX(
VALUES('Date'[Date]),
[HI & Acc Rev]
),
FILTER('poc vFact_SalesLine','poc vFact_SalesLine'[OrderDate_ID] IN DATESINPERIOD('Date'[Date],FIRSTNONBLANK('poc vDim_Customer'[DSE first conversion date],1),-6,MONTH)))
I've tried adding REMOVEFILTERS('Date'[Date]) just before the filtering of order dates, but that doesn't work. Gives me the exact same values as shown below.
All help is very welcome? Is my approach all wrong?

Replicate "Countif" in PowerBI using DAX

I am a new user to DAX and Power BI, but I am familiar with Excel. I want to replicate these countif formulas in DAX. In Excel, they are counting how many times a specific text string (in this case, the name of a brand) appears in the column, for example:
=COUNTIF(BH2:BH31,"Brand_A"), it is counting how many times the text "Brand_A" appears in the selection.
and I would like to know how I can do this in DAX in PowerBI. If anyone would be interested in providing some sample code I could try out, that would be very helpful.
You will likely want something like the COUNTX or COUNTAX function, combined with a FILTER, to replicate the functionality of Excel's COUNTIF.
https://learn.microsoft.com/en-us/dax/countax-function-dax
https://learn.microsoft.com/en-us/dax/countx-function-dax
Eg.
=COUNTAX(FILTER('YourTable',[BrandColumn]="Brand_A"),[BrandColumn])
Power BI's different "COUNT" functions have slightly different criteria in terms of whether a row gets counted or not (based on whether it's considering purely "empty" cells, or how the expression is evaluated), so you'd need to check the docs for each function and work out which one suits your specific requirement
(And by the way, a Google search of "Power BI COUNTIF" will give you plenty of results where you will find a range of different examples that should help)
You can use this calculation (COUNTX may be slow, because its a iterator) :
CountIf = CALCULATE( COUNTROWS('YourTable' ), FILTER(ALL('YourTable'), 'YourTable'[Brand] = "YourBrand"))

how in OBIEE convert rows into columns in analytics

For a certain program I have some type-keywords values like this:
Program Type Keyword
PIM Kind Additional
PIM Period Education
PIM Phase Specialized
PIM Skills Professional
The type is a fixed value, but the keyword depends of the Program and type. I want to transpose this result in analytics by making 4 columns with the type. The result has to look like this:
Program Kind period phase skills
PIM Additional Education Specialized Professional
I have tried by editing the column formula and putting this formula:
CASE WHEN "Type"='Partial period' THEN "Keyword" END
and so on for each different type. But it doesn't give me the result I want. all the new columns are empty.
I also tried with a pivot table, but the keyword isn't a measure, so I don't think this will work.
can someone help?
This simply doesn't make sense in an analytical way. You have no fact, nothing you measure. So no chance of using FILTER...USING... for example.
Don't forget you're not in Excel or a drawing tool. You're in an analytics tool which tries to make sense out of data and not "show data in a weird way".
You have to model things nicely either in the data source itself or be clever in the construction of your RPD.
It's doable in the RPD but it will be quite static and if the list of values changes you will have to adapt it.
tl;dr - garbage data, garbage result

SSRS 2012 total of a percentage measure

I have a percentage measure calculated in SSAS tabular and its correct.
But when i use it in a report i face the following problem (in all percentage measures).
the values under male and female (which are subgroup of saudi and non-saudi) are correct but if you look at the total under "Both sexes" it is incorrect,because it shows summation of male and female percentage, where the right thing is it shows the percentage of the saudi or non-saudi.
If i calculate the measure in SSRS in the expression fields i get it right as follows
But i want to use the measure to show the right values.
So.. can anyone help me with this i have serious issues in later reports and i just cant simply recalculate the SSAS measures in SSRS using expressions.
Note: dont mind the decimal places differences between the two pics.
Thank you.
You can change your MDX query in SSRS to return the subtotal rows then change your Textbox formulas in the body of the Report to say =Aggregate instead of =Sum. This process is described more here by Stacia. You can use manual MDX queries. Don't miss the comment at the bottom:
Romuald Coutaud March 17, 2011 at 7:52 pm Hi Stacia,
In fact, it works in query mode too in 2008R2. But, I have to admit
that the way that RS and AS work together when you want to use
aggregate function is very difficult to perfectly understand and I
have struggle very long time to make one sample report running in this
way. In query mode you need to write MDX by referencing each level of
the hierarchies from the top one to the deepest one want to display in
your report, even you don’t need to use all these ones. As an example
you have to put Year, Quarter and Month to be sure to display Month
and be able to use aggregate function.
Basically in your report =Aggregate will try to retrieve a row where Nationality is not null and Gender is null. If you get stuck please write back with the list of each field used in each group in SSRS and a screenshot of the MDX query designer.

OBIEE remove dimension value based on measure result

One of my dimension contains the different divisions of the company. I have one division which doesn't exist anymore but I want to keep this to check the results of previous years and months.
How do I dynamically eliminate this value of the dimension when it doesn't have a measure anymore?
First I was thinking that adding a filter where the measure should be > 0 or 'not null' but this doesn't seem to do the trick. The dimension keeps showing up.
Thanks!
To go a bit into more details on Robin's question which is the most pertinent one:
Do you try to remove the "obsolete" dimension member from dimension-only queries - like prompts for example?
This is crucial to know as forcing a normally dimension-only query over a fact and thus continuously forcing the inclusion of the fact table for all queries potentially has quite a performance impact.
a) ">0" and "is not null are two completely different things and if used in a cavalier fashion can also yield absolutely wrong results. What if the measure is at a negative value? That would make it "disappear" as well for you. What if there simply is no measure yet but the dimension member is implicitly actually valid just not filled yet? That would also make it disappear for you.
b) How can you know the point of view a user is standing at? For an analysis run for last year this will be perfectly valid to show?
c) What about time-variant comparisons? What about rolling calculations?
d) What about the point-in-time of the dimension rather than the fact? --v
e) What about cross-org analysis like "What do our numbers look like when we apply last year's org vs this year's vs a planned new one which is already loaded into the data source?
While I understand the idea behind your question you should not forget that we're talking analytics here and always face changing dimensions and attributes so your request which seems valid from a human point of view for that one, specific use case is - for the solution as a whole - to be considered wisely.
Although I'm not clear on what you mean by "eliminate this value of the dimension", I think you may be looking for the pivot measure function, called FILTER(Fact USING (Insert your filter here)).
I am assuming that when aggregating using a measure, your problem is that you are unable to exclude the Division that does not exist anymore, and as a result, your measures/aggregations are incorrect.
In this case, you should use the pivot measure as I indicate above.
Your column formula would look like this:
FILTER(Measure1 USING ("SubjectArea1"."Division" <> 'DivisionName'))
Follow these steps to implement this function:
Edit the column formula of your measure.
Highlight the formula of the column and click the Filter... button at the bottom of the Column Formula text box. An Insert Filter window will appear.
Select the Division column from the Subject Areas pane. (Either double-click it, or select it then click the OK button).
You will see a New Filter window. Change the Operator to is not equal to / is not in.
Select your Division that you want to exclude.
Click OK to close the New Filter window.
Click OK again to close the Insert Filter window.
Click OK again to close the Edit Column Formula window.
If your syntax is correct, you will see no errors.

Resources