How to create calculated field in Tableau to return count of instances of value - calculated-field

I'm trying to create a calculated field in Tableau to display a percentage. I have a couple fields that I'm working with: "Record ID" and "Was contact made?" (Yes/No). I want to show the percentage of Record IDs that show up 4 or more times as "Yes" under the field "Was contact made?"
First, I created a calculated field to turn "Was contact made?" "Yes" answers into a 1:
Contact made =1:
CASE [Was contact made?] WHEN "Yes" THEN 1 END
This calculation to get the percentage:
str(countd(if [Contact made = 1] >=4 then [Record ID] end)
/
countd([Record ID]))+"%"
When I put the previous calculation in text, it gives "0%", which is not accurate

You could make an LOD calc called Heavily Contacted? defined as
{ FIXED [Record ID] : SUM(INT([Was contact made?] = "Yes")) >= 4 }
This takes advantage of the fact that the type conversion function INT() converts TRUE values to 1 and FALSE values to 0.
One way to get the value you want could then be
COUNTD(if [Heavily Contacted?]) then [Record ID] end) / COUNTD([Record ID])
That calculation returns a numeric value. I'd just set the default number format for the field to display it as a percentage instead of converting it to a string. (Right click on the field name in the data pane, and look under Default Properties.
With a little more info about your worksheet and problem, you could likely define a field that got the same results without resorting to using COUNTD - as COUNTD can be expensive on large datasets.

Related

Function Module for Calculation Schema in MM

I am looking for a function module that does the calculation schema for arbitrary material.
When opening ME23N and looking for the position details you have the tab Conditions where the table showing contains the base price and various conditions and below the "endprice". But since the price finding calculates the (baseprice + conditions) * amount as the netto value and divides this by the amount this can lead to rounding issues where the calculated value of 4,738 gets rounded to 4,74 which gets stored as netto price. Now when calculating nettoprice * amount this value can be different to the original value printed on the purchase document.
Since the purchase-document-value is not stored in the EKPO my goal is to re-evaluate this value by simply calling a function module with the material number and the calculation schema and any necessary parameter to give me the actual value that (again) is printed on the document.
Is there any function module that can do this or do I have to code the logic by myself?
As I wrote in my comment the solution is the FM BAPI_PO_GETDETAIL1. If you supply the PO number you get several tables containing information that is displayed in the PO create/view transaction. One of them is the iTab POCOND that has all conditions. Then you just have to read this iTab and calculate the values and add them up.
lv_ebeln = 4711
lv_ebelp = 10
" Call FM to get the detail data for one PO and each position
call function 'BAPI_PO_GETDETAIL1'
exporting
purchaseorder = lv_ebeln
tables
pocond = gt_pocond
.
" Loop over the iTab and only read entries for position 10
loop at gt_pocond
into gs_pocond
where itm_number = lv_ebelp.
" Get the netto value NAVS
if ( gs_pocond-cond_type = 'NAVS' ).
lv_netwr = gs_pocond-conbaseval.
endif.
endloop.

IcCube - displaying row numbers in icCube Table

In Google Table there is an option to show the row number. But in the other Tables this option is not available unfortunately. We don't use Google Table, since the IcCube Table is just rendering way faster with lots of data.
One workaround we learned about would be to create a calculated measure with a constant value for example : Row as 42 and then setup a custom renderer as following:
function(context) {
return "<span>"+context.getRowIndex()+"</span>";
}
Unfortunately if the table is exported, the constant value (i.e. 42) is displayed and not the row number.
Is there a useful way to get row numbers in the other tables in icCube?
One possibility is to use a calculated member to get the line numbers (it's not going to work with drilldowns).
Assuming you've two axis, the query would look like :
WITH
FUNCTION _currentTuple(_t) as CASE _t.count
WHEN 1 THEN _t.hierarchy.currentMember
WHEN 2 THEN ( _t.Item(0).hierarchy.currentMember,_t.Item(1).hierarchy.currentMember)
// you get the idea
ELSE Error( "_currentTuple- " + Str(_t.count) )
END
MEMBER [Line Position] as Rank( _currentTuple( Axis(1)(0) ) , Axis(1) )
SELECT
[Line Position] on 0,
[Customers].[Geography].[Region].members * [Product].[Product].[Category] on 1
FROM [Sales]
Which make us think we should add a function to get the current axis members in an easier way.
_hope it helps

SAP BO - how to get 1/0 distinct values per week in each row

the problem I am trying to solve is having a SAP Business Objects query calculate a variable for me because calculating it in a large excel file crashes the process.
I am having a bunch of columns with daily/weekly data. I would like to get a "1" for the first instance of Name/Person/Certain Identificator within a single week and "0" for all the rest.
So for example if item "Glass" was sold 5 times in week 4 in this variable/column first sale will get "1" and next 4 sales will get "0". This will allow me to have the number of distinct items being sold in a particular week.
I am aware there are Count and Count distinct functions in Business Objects, however I would prefer to have this 1/0 system for the entire raw table of data because I am using it as a source for a whole dashboard and there are lots of metrics where distinct will be part/slicer for.
The way I doing it previously is with excel formula: =IF(SUMPRODUCT(($A$2:$A5000=$A2)*($G$2:$G5000=$G2))>1,0,1)
This does the trick and gives a "1" for the first instance of value in column G appearing in certain value range in column A ( column A is the week ) and gives "0" when the same value reappears for the same week value in column A. It will give "1" again when the week value change.
Since it is comparing 2 cells in each row for the entire columns of data as the data gets bigger this tends to crash.
I was so far unable to emulate this in Business Objects and I think I exhausted my abilities and googling.
Could anyone share their two cents on this please?
Assuming you have an object in the query that uniquely identifies a row, you can do this in a couple of simple steps.
Let's assume your query contains the following objects:
Sale ID
Name
Person
Sale Date
Week #
Price
etc.
You want to show a 1 for the first occurrence of each Name/Week #.
Step 1: Create a variable with the following definition. Let's call it [FirstOne]
=Min([Sale ID]) In ([Name];[Week #])
Step 2: In the report block, add a column with the following formula:
=If [FirstOne] = [Sale ID] Then 1 Else 0
This should produce a 1 in the row that represents the first occurrence of Name within a Week #. If you then wanted to show a 1 one the first occurrence of Name/Person/Week #, you could just modify the [FirstOne] variable accordingly:
=Min([Sale ID]) In ([Name];[Person];[Week #])
I think you want logic around row_number():
select t.*,
(case when 1 = row_number() over (partition by name, person, week, identifier
order by ??
)
then 1 else 0
end) as new_indicator
from t;
Note the ??. SQL tables represent unordered sets. There is no "first" row in a table or group of rows, unless a column specifies that ordering. The ?? is for such a column (perhaps a date/time column, perhaps an id).
If you only want one row to be marked, you can put anything there, such as order by (select null) or order by week.

Read (lookup multiple criteria) records from database table, and Write to database table with matching criteria

How do I replicate the equation below in Access Query or in VB.net code?
{=SUM(IF('Customer '!$G$3='Glass Fabrication'!$D$3:$G$3,IF("Hole"='Glass Fabrication'!$A$4:$A$15,'Glass Fabrication'!$D$4:$G$15)))*E3}
The above equation is what I use in excel to do Vlookup more than 1 value and returns a value that matches 2 criteria. (Now, this can go on forever, and have as many criteria as possible)
Now, I am working on VB.net to make a Quotation system with the MS Access database (.mdb).
I have a table that lists all products that have several identifiers with 4 different prices. For the simplicity, let's say I have 2 identifiers and 4 prices.
I have fields like Name Category as identifiers, and sample data looks like this.
Name Category Tier 1 Tier 2 Tier 3 Tier 4
Apple...Fruit............$2..........$2...........$1.7........$1.5
Apple...Juice............$1..........$1...........$.75........$.75
Orange..Fruit.........$1.8.......$1.8.........$1.5.........$1.3
Coke...Drink............$1..........$1...........$.75........$.75
User Input = Apple , Juice , Tier 1
I can make =Sum(If formula in excel to check for Name column to match selected value, then check the Category column to check the 2nd value, then check the 1st Row header for Tier 1 value and returns $1.
I am very new to Access Database, so I have a hard time how to replicate this. I have tried making a query, but it only "filters" such criteria, and doesn't return a value.
Ultimately, I would want a table/query called Shopping Cart that accepts user input such as "Apple, Juice, Tier 1" and 4th Column(read-only) would automatically calculate and find that Price of $1 by looking up those 3 things.
I have set up the shopping cart table already, and it works perfectly except I don't know how to include this read-only automatically calculated/vlookup column. (I am using DataGridView in VB.net to bind to a shopping cart table that takes 3 user inputs.)
What would be the best way to do this? Make Query in Access and make it calculate in the database itself or VB.net to handle the calculation and write into the 4th column everytime user inputs 3 values?
Try this SQL query in MS Access, provided your example data is saved as an Access table:
SELECT Table.Name, Table.Category, [Enter Tier],
IIF([Enter Tier] = 'Tier 1', Table.[Tier 1], IIF([Enter Tier] = 'Tier 2', Table.[Tier 2],
IIF([Enter Tier] = 'Tier 3', Table.[Tier 3], IIF([Enter Tier] = 'Tier 4', Table.[Tier 4], Null)))) As Price
FROM Table
WHERE Table.Name = [Enter Name] AND Table.Category = [Enter Category]
The Enter fields will pop up for user to fill as parameter requests when you open query. It would be better to use an Access form for user to fill unbound textboxes and command button opens query using textbox values. Form fields can replace above Enter fields.
Dim row AS DataRow = DataTable.Select("ColumnName1 = 'value3'").FirstOrDefault()
If Not row Is Nothing Then
searchedValue = row.Item("ColumnName2")
End If
Here value3 is a constant value. To use objects(variables),
Dim row AS DataRow = DataTable.Select("ColumnName1 = '" & variable & "'").FirstOrDefault() was used.

How to Calculate specific cell total in SSRS?

As shown below image i have problem to calculated specific fields total.
I want to just calculate only two cells value like
total=(total opening-qty) + (total purchase-qty) cell and opening, purchase, sales are all from Type Group.
Please tell me the Expression or tips.
It's hard to tell without seeing your DataSet/report, but the expression would be similar to:
=Sum(IIf(Fields!Type.Value = "OPENING" or Fields!Type.Value = "PURCHASE"
, Fields!qty.Value
, Nothing)
This will take a SUM of all qty values, but it will ignore any Type rows that don't match the types you're specifying, i.e. OPENING and PURCHASE.
The most import thing is to make sure the expression is in the correct Scope, i.e. if you're grouping by Type the expression should be applied outside of that group Scope to make sure the expression is considering all the required rows.