Calculate product rate of previous day in tableau - tableau-api

I have a sample dataset:
DATE: 11-01-2015, 12-01-2015, 13-01-2015
SALE: $120, $0 , $100
In tableau I want if Today's sale is 0 as given above for 12-0-2015 then previous day sale should be considered as today's sale.
Can anyone help?

Create a calculated field with this formula:
IF SUM([Sale])=0
THEN LOOKUP(SUM([Sale]),-1)
ELSE SUM([Sale])
END
And compute using Date.

IF FIRST() <> 0 AND SUM([Sale])=0 THEN LOOKUP(SUM([Sale]), -1)
ELSE SUM([Sale]) END
Setting Compute Using to Date
Checking First() in the condition handles the case when the first day had zero sales

Related

How to calculate total sales as of first day of the current month as Previous month sales in power BI

I have salesamount column and the saledate column in my table. so I need to calculate total sales for each month based on below calculation.
total sales for current month=sum(salesamount) as of 1st day of the next month
for example sales of December-2021 should be calculated based on the total sales as on Jan-1-2022. total sales for January should be blank until Feb-1-2022 as it should be the total sales as on feb-1-2022. I am very much confused how we can achieve this in Dax. Really appreciate your help.
If I understand your question correctly, you can use the following DAX measure:
Total Sales =
var currentDate = MAX(myTable[saleDate])
var firstOfMonth = DATE(YEAR(CALCULATE(MAX(myTable[saledate]), ALL(myTable))),
MONTH(CALCULATE(MAX(myTable[saledate]), ALL(myTable))), 1)
var result = SUM(myTable[salesamount])
Return IF(currentDate < firstOfMonth, result)
This will take the current date of the report context and compare it to the 1st of the current month. If the currentDate is less than the 1st of the current month, the result will be calculated.
If your dataset has a Date table, you can replace the 'myTable[saledate]' references with a reference to the Date column in your date table.
With some sample data, here are the results:
(I added the firstOfMonth column for demonstration purposes)

Tableau Calculated Field of Running Average for the last month

Hey Guys I am trying to do a calculation for a Running Average over the last month in a calculated field in Tableau. However I struggle with defining the last month range and adding it to the calculation. I thought of using
IF [date] >= DATEADD('month', -1, TODAY()) THEN
RUNNING_AVG(SUM( IF [Entry Type] = 'Sale'
THEN [Invoiced Quantity]
ELSE 0
END )) END
However this is not working at all. I hope someone can help me out. Cheers
This worked out:
(SUM( IF [Entry Type] = 'Sale' AND [Posting Date] >= DATEADD('month',-1,TODAY())
THEN [Invoiced Quantity]
ELSE 0
END
))
You do not have to write this into calculated field. Just filter date column and set that to be always filter for last 30 days, or last month. Then, use "calculated field" section just for calculation independently from the "date" which is already considered in filter section.
Good luck

Calculate the percent difference between two percentages at two dates in Tableau

The goal is to create a column that shows the percent difference between to percentages.
Tried creating many calculated fields around
-first() - last() -- this just calculates the week number difference
-if date = #2019-02-03# then sum number of records else null end -- invalid
-case when date = #2019-02-03# then sum number of records else null end -- invalid
The 'filter 1' column is where I'd like to display this information.
Expected result:
row 1 in filter 1 should = 12% (absolute percentage change of -5 and 7)
Any help is super appreciated!
I figured this out in two ways:
one created two date fields - one for one week and one for the last week
: then made a diff % measure and used these as the columns.
There's another way, more gnarly:
if first() == 0 then
(
window_sum((if last() == 0 then sum(count_measure) end))
-(window_sum(if first()==0 then sum(count_measure) end))
)
/(window_sum(if first()==0 then sum(count_measure) end))
end

Tableau - Max(day(var_data)) for a month condition

I have 4 dates in a month (4 weeks)
like:
var1
2001-01-10
2001-01-15
2001-01-20
2001-01-30
2001-02-10
2001-02-15
2001-02-20
2001-02-30
I want to creat a new var with just the max date in this month:
like:
var2=max(var1) for the same month
0
0
0
2001-01-30
0
0
0
2001-02-30
how can i do this?
I think I understand what you're after, do you only want to show the max DAY for a given month, for every month?
If so, create a table calculation with a formula like this:
{FIXED DATETRUNC('month',[date field]): MAX([date field])}
To explain:
FIXED = apply the aggregate function to only these 'windows' for the calculation. Here we're fixing at the month level. But you could do several layers like month, category, subcategory
: = after the colon you can apply the calculation that you want to do, in your case it's the maximum month
So bringing that together in a sentence, what we're doing is "For each date, truncate to the month level, then for each month, what's the maximum date?"
Hope that helped

How to dynamically calculate the YTD for current year without using a table calculation in Tableau?

How do I dynamically calculate the Year To Date (YTD) for the current year without using a table calculation in Tableau?
I have used the below formulas to calculate YoY for the current year:
if datediff('year',[Date],TODAY())=0 then [Sales] END
For the previous year:
if datediff('year',[Date],TODAY())=1 then [Sales] END
YoY:
sum(current year)/sum(previous year)-1
create a calculated field:
[date] >= MAKEDATE(Year(today()),1,1) and
[date]<= today()
Drag this to filter and select True
It depends on what you're trying to achieve. If you want to filter dates to show only values in the current year without a table calculation, then you could create a calculated field like below and filter on the result:
if Year([Date]) = YEAR(TODAY()) then "YTD" else "Not" END