Create a PowerBI measure to sum a text column - powerbi

I have a column in my table that looks as follows
Table
Answer Type
10 | 10
N/A | 3
four | 3
20 | 10
4 | 10
yes=1 | 3
I only want to sum the columns that have a type of 10
This is what I tried.
sum = CALCULATE(SUM('Table'[Answer]),filter('Table',Table[Type]=10))+0
I wanted the output to be a sum of 34, but I keep getting an error:
The function sum can not work with values string
I can not change the datatype of the column as I use the text for a different calculation.
Could some one advise how to achieve this?

You can try below code
Only_int_sum =
CALCULATE (
SUMX ( 'Table', IFERROR ( VALUE ( 'Table'[Answer] ), 0 ) ),
'Table'[Type] = 10
)
PFA screenshot for reference

Related

Sum previous rows in column

I am looking for a solution to sum previous rows in a calculated column.
Sample data:
product | sales | cumulative
000001 | 2000 | 2000
000001 | 2000 | 4000
000002 | 1500 | 1500
000001 | 2000 | 6000
000002 | 1500 | 3000
Could anyone help me with the DAX please.
This is what I would do
step 1)
I would create a new ordinal column with a sequential number for each product group
index =
RANKX (
FILTER (
Sales,
EARLIER ( Sales[Product] ) = Sales[Product]
),
Sales[Sales],
,
ASC
)
Step 2)
I create the Running Totals column that shows the running totals by group
Running Totals =
CALCULATE (
SUM (Sales[Sales]),
FILTER(
Sales,
Sales[Product] = EARLIER ( Sales[Product])
&& Sales[index] <= EARLIER ( Sales[index])
)
)
this is the result

Dax filter table desc with running total

I have table with profit, dev_num
I want to filter that table desc by profit column where running sum of dev_num =<30
i mean sth like this:
Profit | Dev_num
1000000 10
100000 10
340000 6
240000 4
do you know maybe how i should build my measure for this calculated table ?
Your DAX can be like below :
EVALUATE
FILTER (
ADDCOLUMNS (
VALUES ( 'yourTable'[Profit] ),
"Dev_num", CALCULATE ( SUM ( 'yourTable'[Dev_num]) )
),
[Dev_num] <= 30
)
ORDER BY 'yourTable'[Profit]

Calculate rolling average and use for missing values

I am trying to build a model in PowerBI that forecasts future values based certain inputs. The issue I am running into is that some of my data is missing rows, and I also need to use the last value of data as the one for the next period, to which I will add additional (calculated) values. I used an index that starts at -5 and goes through 30, and added a date column expressed as Today()+Index. The data looks like this:
Index | Date Index | Values |
------------------------------
-6 11/4 2
-5 11/5 5
-4 11/6 7
-3 11/7
-2 11/8 5
-1 11/9 4
0 11/10 <-- This is today
1 11/11
2 11/12
...
What I need the data to look like is this:
Index | Date Index | Values |
------------------------------
-6 11/4 2
-5 11/5 5
-4 11/6 7
-3 11/7 4.667
-2 11/8 5
-1 11/9 4
0 11/10 4 <-- This is today
1 11/11 4
2 11/12 4
...
The Dax Formula I have is:
Values =
If(
AND(
SUMX(
Filter(
Table 2,Table 2[Date]='Summary Table'[Date Index]
),
Table 2[Initial Values]
)=0,
'Summary Table'[Date Index]<Today()
),
Calculate(
Averagex(
Table 2,Table 2[Initial Values]
),
DATESINPERIOD(
Table 2[Date],Today()-1,5,DAY
)
),
SUMX(
Filter(
Table 2,Table 2[Date]='Summary Table'[Date Index]
),
Table 2[Initial Values]
)
)
Note that I am pulling in my values from a different table, Table 2, which has multiple dates with values, which I am summing in this summary table by date. For some reason, the formula is filling in an incorrect average value for missing values, and not pulling the last value (from Today()-1) through to the end of the data. Any help or advice would be appreciated!

dax calculation for costume measure

i have a 2 tables that looks like this
Key |Num Of Treatments| Cost |
1 2 1000
1 2 1500
1 2 2000
2 3 700
3 3 800
4 4 900
key | limit |
1 1
2 1
3 2
4 3
the calculation that i want to do on dax is : (Num Of Treatments-Limit)*cost/Num Of Treatments
Assuming that the key column is unique for the second table (Table2 in dax).
Calculation =
VAR _limit =
LOOKUPVALUE ( Table2[limit], Table2[key], [key] )
RETURN
DIVIDE ( ( [Num Of Treatments] - _limit ) * [cost], [Num Of Treatments] )
This can be easily be achieved after creating one to many relationship between two tables with column Key.
Dax formula :
New Measure = ((SUM(Asset[No Of Treatments])-SUM(Tickets[Limit]))*SUM(Asset[Cost]))/SUM(Asset[No Of Treatments])

Lookup previous value based on criteria from another column

Objective:
Get the previous value based on a criteria.
Situation:
I have a table with groups numbered 1,2. I would like to look at the previous value (referring to the previous date) but for each group.
Desired Output:
My output should look like this
+------------+-------+-------+----------------+
| date | group | value | previous value |
+------------+-------+-------+----------------+
| 2019-02-02 | 2 | 50 | 45 |
| 2019-02-02 | 1 | 60 | 80 |
| 2019-01-18 | 2 | 45 | |
| 2019-01-18 | 1 | 80 | |
+------------+-------+-------+----------------+
What I tried:
previous value =
LOOKUPVALUE(
Table[value],
Table[date],
CALCULATE(
MAX(Table[date]),
FILTER(
Table,
Table[group]=EARLIER(Table[group]) && Table[date]<EARLIER(Table[date])
)
)
)
As I understand, you want this as a calculated column, not a measure. Try:
Previous Value =
VAR Current_Date = Table[date]
VAR Previous_Date =
CALCULATE (
MAX ( Table[date] ),
Table[date] < Current_Date,
ALLEXCEPT ( Table, Table[group] )
)
RETURN
CALCULATE (
MAX ( Table[value] ),
Table[Date] = Previous_Date,
ALLEXCEPT ( Table, Table[group] )
)
How it works:
We iterate each record of the Table and store its date in "Current_Date" variable.
For each record, find previous date, which is the max date that is smaller than the date of the record we are iterating. To do that, we need to have access to all dates, not only the date of the current record, so we need to use ALL function. However, since we need to do it by group, we use ALLEXCEPT, which preserves filter for the current group.
Once previous date is found, you can use exactly the same pattern to find previous value - find MAX value where record's date equals previous date, while preserving group filter.