How to sum two columns if one column might have two different variables which would call for a different calculation - if-statement

I need to create a SUM() column (summing two columns), the issue I'm having in the second column might have a result which needs to multiply the first column or it might have a result which needs to be added to the result in the first column.
Example
First Column (Estimated Costs)
50,000
100,000
5,000
Second Column (contingency amounts)
5%
10,000
10%
5,000
The first column will always have numbers. The second column will either have a percentage or an amount. I need to create a formula to sum either possibility. Is this possible or do I need to create two different sum columns for either scenario?
I tried using an OR statement with the SUM() formula but it didn't seem to work.

I understand that you're providing the cotingency cost of the estimate on 2 different ways as a percentage of the estimate or as a lumpsum amount to be added to the estimate.
You should use IF() to differentiate the two operations you have to apply. As the two types of values are very different on the contingency column the #BigBen suggestion of Value>1 should work perfectly well as you won't be adding less than $1 buck as a contingency. Therefore the formula will be:
=IF(B1>1,A1+B1,A1*(1+B1))
Column A is Estimated Costs, Column B is Contingency.

Related

Divide two measures from two different tables

I have to divide two measures from two different tables. I have created a measure in Table A & created measure-2 in Table B.
When I use matrix visual in Power BI by taking date field in columns and region in rows (for table A&B), I can see the both table values are correct as I am expected.
Ex: Table A 2017-Q1 value by measure1 is 29.2, Table B 2017-Q1 value by measure1 is 2.9.
I have to divide both measures and I need to show the value (divide%) in TableA along with Measure1.
Unfortunately I tried in multiple ways by forming relationship b/w two tables also, But not getting the expected result i.e., 29.2/2.9 we should get 10% but instead of that getting 3%.
Without knowing your data model, it's hard to give a reasonable answer.
https://learn.microsoft.com/en-us/dax/related-function-dax
Your best change of understanding what happens is to learn up on relations, and changes them when needed. The documentation is a great starting point.
Unrelated data plotted in a visual of different data will always aggregate since there is no relation to split your values. The value of 3% is correct, your assumption that you want 10% as an outcome is not valid for your situation.
If you link the dates of table A and the dates of table B to a seperate Calendar, it all would work.

(SOLVED using VLOOKUP) Tiered/Nested IF clauses within one cell

Imagine a tiered revenue sharing scheme like this:
Revenue up to 10000 get 100% of it.
Revenue up to 12000 get the above plus 80% of the amount above 10000.
Revenue up to 14000 get the above plus 60% of the amount above 12000.
Revenue up to 16000 get the above plus 40% of the amount above 14000.
Revenue over 16000 get the above plus 20% of the amount above 16000.
E.g. A revenue of 13000 will get you a share of 10000+0.82000+0.61000 = 12200.
I tried making a table (each threshold a column) and calculate the individual fractions using IF clauses and then add them all up. It is very cumbersome. I would like to use only two cells with the entire calculation done in one cell, hard-coded.
If at all possible, extra bonus points if I can have the threshold values (10000, 12000, etc) and fractions (100%, 80%, etc.) in separate cells as parameters for the calculation, maybe something like an array-function?
Thank you very much in advance!
Start your lookup table with a value of 0 and a rate of 100%. In this case, VLOOKUP() with the last parameter equal to 1 will correctly find the required row.
In order not to recalculate all the above rows for each of the values, calculate them in advance and place them in the table as an additional column.
For the first line it will be 0, and calculate all subsequent values using a formula like =C2+(A3-A2)*B2
For such a table, a not very complicated formula will return the correct result:
=(<revenue>-VLOOKUP(<revenue>;<lookup_table>;1;1))*VLOOKUP(<revenue>;<lookup_table>;2;1)+VLOOKUP(<revenue>;<lookup_table>;3;1)
The third parameter in the VLOOKUP() functions increases from left to right: 1 - the base amount, 2 - the interest rate, 3 - the calculated markup for reaching the previous levels.
For the data shown in the figure, the formula is used
=(E2-VLOOKUP(E2;$A$2:$C$7;1;1))*VLOOKUP(E2;$A$2:$C$7;2;1)+VLOOKUP(E2;$A$2:$C7;3;1)

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

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

in Dax, how do I row match by some categorical column instead of getting a single value when using a numerical variable from another related table

How can I use the values of a variable from a table B in a related table A in a such a way that it can be matched to each row of table A correspondingly?
I need to calculate a ratio as this
sum(A[event_number])/sum(B[client_number]
The 2 variables are from different related table. The relation between the tables are one (A) to many (B).
When I put this ratio on a matrix constructed with variables and measures from table A where the rows are stores, the denominator should be the client number per store but I only get the sum of all clients instead.
For example if a store "ASD" has 5 events it should get divided by 20 which are the clients related to that store and not 500 which is the sum of all clients for all stores.
I have tried using related when calculating the ratio, and allexcept to create a column of number of clients in A but nothing has given the expected result. Please help.

Cant measure from date column

I'm trying to count days between a date from the column 'completionDate' and today
The table name is 'Incidents (2)'
I have a simuler table called 'incidents' here it's working.
The code:
DaysClosed = DATEDIFF('Incidents (2)'[completionDate].[Dag];TODAY();DAY)
The error i get:
'A single value for variaton 'Dag' for column 'completionDate' in table 'Incidents (2)' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.'
The error you get strongly depends on how you are evaluating your formula, that's why it might work on another table but not on this one. As #JBfreefolks pointed out correctly you are specifying a column where a scalar value is expected. That can work depending on the context you are evaluating your formula over (assuming it is a measure).
For instance, imagine a data-set with 100 rows equally divided into four categories A,B,C,D. When you create a table visual with a row for each category, each row will have 25 underlying records that will be used in any calculation added to this row (either a measure or an aggregate of any value). This means that when using a formula like datediff with a column reference, it will get 25 values for it's second argument where it expects one.
There are several ways to solve the problem depending on your desired result.
Use a measure like MAX like #JBfreefolks suggested to make sure that a single value is selected from multiple values. The measure will still be calculated over a group of records but will summarize it by taking the maximum date.
Make sure the visual you are using has something like an ID in there so it doesn't group, it displays row context. Any measure added to this visual will evaluate in row context as well.
Use a calculated column instead. They are always evaluated in row context first and their values can be aggregated in a visual later on. When using TODAY() , you probably need to refresh your report at least every day to keep the column up to date.
A more complicated way is to use an iterator like SUMX() or AVERAGEX() to force row context evaluation inside of a measure.
Good to see you solved it already, still posting as it might be helpful to others.
'Incidents (2)'[completionDate].[Dag] referencing a colomn. It is in your computation returning a table (multiple date in the evaluation context) instade of a scalar needed in DATEDIFF calculation.
You need to leverage to be sure that 'Incidents (2)'[completionDate].[Dag] is rturning a scalar value. To do that you can leverage rowcontext and then also formula like Max.