setting date range filter in saved search formula - netsuite

I’m looking for some guidance on how to write date range filter within saved search formula. For example, below is what I have so far to see sales of certain type, but also need to add date filter so get only sales for this year to date. I understand I can set date filter in filter section, but I also need to add other columns in this report which will display transaction amounts of different types and dates. Thanks in advance for any input.
Formula:
CASE WHEN {transaction.type} = 'Invoice' AND {transaction.custbody1} = 'Direct' THEN {transaction.amount} end

You can use the SQL TO_CHAR function to return text portions of a date, for comparison with the corresponding text portions of sysdate. In the example you give, that would look like:
CASE WHEN {transaction.type} = 'Invoice'
AND {transaction.custbody1} = 'Direct'
AND TO_CHAR({trandate}, 'YYYY') = TO_CHAR(sysdate, 'YYYY')
THEN {transaction.amount}
END
You can use the same approach with different formats to get almost any date. You could also use EXTRACT to get slightly better performance in some situations. EXTRACT returns a number rather than text.
CASE WHEN {transaction.type} = 'Invoice'
AND {transaction.custbody1} = 'Direct'
AND EXTRACT(YEAR from {trandate}) = EXTRACT(YEAR FROM sysdate)
THEN {transaction.amount}
END
You may find TO_CHAR to be easier to use when you want to combine different parts of a date. E.G.: TO_CHAR({trandate}, 'YYYY-MM') = TO_CHAR(sysdate, 'YYYY-MM') rather than EXTRACT(YEAR from {trandate}) = EXTRACT(YEAR from sysdate) AND EXTRACT(MONTH from {trandate}) = EXTRACT(MONTH from sysdate)

Related

Power Query excel

i am trying to use a if formula in power query. For example, if the Column contains “guy” then value is male and the false value is “female”. I tried different ways and I can’t find the right formula to use in power query. Can anyone help me please?
If you are entering this into the Add Custom Column dialog, something like (for case-insensitive):
= if Text.Contains([Column],"guy", Comparer.OrdinalIgnoreCase) then "male" else "female"
It sounds like you are wanting to conditionally replace values in a column, based on the existing values in that column? If so, you can use the Table.ReplaceValue function in Power Query:
= Table.ReplaceValue(Source,
each [Gender],
each if [Gender] = "guy" then "male" else "female",
Replacer.ReplaceText,{"Gender"})
That will change all values of "guy" to "male', and ALL other values to "female", as you stated.
You can also leave values in place that don't meet the criteria, by simply referencing the column name instead of a specifying a new value:
= Table.ReplaceValue(Source,
each [Gender],
each if [Gender] = "guy" then "male" else [Gender],
Replacer.ReplaceText,{"Gender"})
Create a table with a column called Gender, and load it to power query. Right-click on the column header and choose Replace Values to get the UI to build your statement for you, then replace the generated code with the above modification(s) and apply to your actual requirements. The key is using the each expression to tell Power Query to test at the row value level. If you omit each, you'll see the error:
"Expression.Error: There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?"
= Table.AddColumn(#"Changed Type","ColumnName",each if[Column] ="""Guy""" then"""Male""" else"""Female""")

Sum from two tables with filters in DAX/Power BI

I've got two tables.
The 1st one contains SKU, client, order date, order amount.
The 2nd one contains SKU, client, promo id, promo start date, promo end date.
I need to calculate ordered amount for each client-promo-SKU.
In Excel I would have something like:
SUMIFS(
TABLE1[ORDERED],
TABLE1[CLIENT], TABLE2[CLIENT],
TABLE1[ORDER DATE], ">="&TABLE2[ORDER START DATE],
TABLE1[ORDER DATE], "<="&TABLE2[ORDER END DATE],
TABLE1[SKU], TABLE2[SKU]
)
Table 1
Table 2
Excel formula. Cells K2 and K3
I've already figured out how to filter the first table by date (the connection between ORDERS and PromoLib tables is SKU, many to many):
ordered = CALCULATE(
SUM(ORDERS[order qty]),
KEEPFILTERS(DATESBETWEEN(ORDERS[Document Date], PromoLib[Order Start],
PromoLib[Order End])),
)
How can I add filters to this DAX formula so it will filter the ORDERS table by client?
The best practice would be to have a Clients dimension table, a Dates dimension table, an Orders fact table and a PromoLib fact table. Then the measures would be much simpler. That being said, here is the syntax for the CALCULATE function:
CALCULATE(<expression>[, <filter1> [, <filter2> [, …]]])
You can add as many filter arguments as needed, so you could add another argument for a client filter like this:
FILTER (Orders, Orders[Client] = PromoLib[Client])

How can I split weekly data to monthly using Excel/ Power Pivot?

My Data is in weekly buckets. I want to split the number into a monthly number but, since there is an overlap in days falling in both the months, I want a weighted average of the data in terms of days that fall in each of the months. For example:
Now, in the above picture, I want to split that 200 (5/7*200 in Jan, 2/7 in Feb). How can I do that using Excel/ Power Pivot/ Dax Functions? Any help here is much appreciated.
Thank you!
Assuming your fact table looks something like below. Values are associated with the starting date of the week it occurred.
Although it may actually be a more granular data, having multiple rows for each week with additional attributes (such as identifiers of a person, a store, depending on the business), what being shown below will work the same.
What we need to do first is to create a date table. We can do that in "Design" tab, by clicking "Date Table", then "New".
In this date table, we need to add a column for starting date of the week which the date of each row is in. Set the cursor to "Add Column" area, and input following formula. Then rename this column to "Week Start Date".
= [Date] - [Day Of Week Number] + 1
Now, we can define the measure to calculate the number allocated to each month with following formula. What this measure is doing is:
Iterating over each row of the fact table
Count the number of days for the week visible in the filter context
Add the value portion for the visible days
Value Allocation := SUMX (
MyData,
VAR WeekStartDate = MyData[Week]
VAR NumDaysInSelection = COUNTROWS (
FILTER (
'Calendar',
'Calendar'[Week Start Date] = WeekStartDate
)
)
VAR AllocationRate = DIVIDE ( NumDaysInSelection, 7 )
RETURN AllocationRate * MyData[Value]
)
Result in the pivot table will be looking like this.

Get last item with date range and name filter in google sheets

I have the below set of records in Google Sheets. I would like to filter the rows with specific name and date range. Once I have the filtered data, I would like to fetch the last row's final amount cell data.
Ex: I would like to fetch final amount as 300 if my date(dd/mm/yyyy) range is 01/01/206 to 11/06/2016 and Name selection is 'Sandeep'.
As I have experience SQLite db, I have inserted the same records in DB and got the expected result using the below query.
select Final from MyTable where Date in (select max(Date) from MyTable WHERE Date BETWEEN '01/01/2016' AND '11/06/2016' and name = "Sandeep")
But I am not getting idea how to use multiple select statements in google sheets. It is ok for me to get result using any other way. So please help me to get the result as explained above.
= QUERY (A1:E50,"Select F where A > date '2016-1-1' and A < date '2016-6-11' and B ='Sandeep' order by A desc limit 1")
Use Column IDs A,B,C instead of name, income. Multiple columns can be given in a single Select clause separated by a ,
Dates in where clause should be written in yyyy-mm-dd format only(regardless of the format of dates in actual column)
See if this works
=index(E:E, max(filter(row(A:A), A:A>date(2016, 1, 1), A:A<date(2016, 6, 11), B:B="Sandeep")))
If you want to include start and end date, change > to >= and < to <=.

Excel Power Query - from web with dynamic worksheet cell value

We have a spreadsheet that gets updated monthly, which queries some data from our server.
The query url looks like this:
http://example.com/?2016-01-31
The returned data is in a json format, like below:
{"CID":"1160","date":"2016-01-31","rate":{"USD":1.22}}
We only need the value of 1.22 from the above and I can get that inserted into the worksheet with no problem.
My questions:
1. How to use a cell value [contain the date] to pass the date parameter [2016-01-31] in the query and displays the result in the cell next to it.
2. There's a long list of dates in a column, can this query be filled down automatically per each date?
3. When I load the query result to the worksheet, it always load in pairs. [taking up two cells, one says "Value", the other contains the value which is "1.22" in my case]. Ideally I would only need "1.22", not the title, can this be removed? [Del won't work, will give you a "Column 1" instead, or you have to hide the entire row which will mess up with the layout].
I know this is a lot to ask but I've tried a lot of search and reading in the last few days and I have to say the M language beats me.
Thanks in advance.
Convert your Web.Contents() request into a function:
let
myFunct = ( param as date ) => let
x = Web.Contents(.... & Date.ToText(date) & ....)
in
x
in
myFunct
Reference your data request function from a new query, include any transformations you need (in this case JSON.Document, table expansions, remove extraneous data. Feel free to delete all the extra data here, including columns that just contain the label 'value'.
(assuming your table of domain values already exists) add a custom column like
=Expand(myFunct( [someparameter] ))
edit: got home and got into my bookmarks. Here is a more detailed reference for what you are looking to do: http://datachix.com/2014/05/22/power-query-functions-some-scenarios/
For a table - Add column where you get data and parse JSON
let
tt=#table(
{"date"},{
{"2017-01-01"},
{"2017-01-02"},
{"2017-01-03"}
}),
add_col = Table.AddColumn(tt, "USD", each Json.Document(Web.Contents("http://example.com/?date="&[date]))[rate][USD])
in
add_col
If you need only one value
Json.Document(Web.Contents("http://example.com/?date="&YOUR_DATE_STRING))[rate][USD]

Resources