Calculated Items in Excel: How to check if a field contains a particular item - vba

As part of my VBA script I'm trying to add a calculated item to a pivot field as follows:
myPivotTable.PivotFields("Validity").CalculatedItems.Add "Valid %", "=ROUND(('Rejected'+'Duplicate')/('Valid'+'Rejected'+'Duplicate') * 100, 1)"
In my source data table amongst other columns I have a 'Validity' column (the name of the field) with rows of data that can contain the values 'Valid' or 'Rejected' or 'Duplicate'. The above line will work fine when the source data column contains at least one row for each of those values.
Sometimes though my data set doesn't contain a row with 'Duplicate' so I get an error 400 when this is run.
I tried to use the IFNA, IFERROR functions to try and return 0 like this but this produces the same error:
myPivotTable.PivotFields("Validity").CalculatedItems.Add "Valid %", "=ROUND(('Rejected'+ IFNA(0, 'Duplicate'))/('Valid'+'Rejected'+ IFNA(0, 'Duplicate')) * 100, 1)"
How can I fix this?

Related

Create column from another column with multiple values in the rows

I want to filter values but the rows contain multiple values. So here is an example of what I want to create.
I want to create a column where rows that contain multiple values equals to 1 value.
Example:
A row that contains Konsulent, Hovedsertifisør, Hovedkonsulent, konsulent to respond to just Hovedsertifisør for example.
I tried the related dax, if else statements
As given in your example I assume that the "," defines multiple values, not the " ".
Use this Calculated Column
Column =
IF(
SEARCH(",", 'Table'[Name], 1, BLANK()),
"Hovedsertifisør",
'Table'[Name]
)

SQL Query - Return rows where based on dual conditioning of same column

I am struggling to figure this out. I have a column, "Column1" that is VARCHAR(max) and contains a lot of raw text. I want to filter out rows based on this column not having a particular word in the column UNLESS a secondary condition is met.
For example:
Return all rows where the word "Final" does not exist. However, if the word "Final" does not exist but the word "Ongoing" does exist, then I need to return this row and not hide it.
Here is my code (not working)
SELECT *
FROM ThisTable
WHERE (
Column1 NOT LIKE '%Final%'
OR Column1 LIKE '%Ongoing%'
)
Here are some examples of data that exists, and whether the row should be returned.
Row 1: "this project has ended" (return this row)
Row 2: "this project has ended but is ongoing" (return this row)
Row 3: "this project is final." (do not return this row)
Row 4: "This project is final and ongoing" (return this row)
Your current query is filtering out records where neither 'final' nor ongoing are contained, but as I understand it you want to return everything apart from records that include 'final' but don't also contain 'ongoing'.
I think something like this should work:
WHERE NOT (Column1 LIKE '%Final%' AND Column1 NOT LIKE '%Ongoing%')

Powerpivot using If function

I have a table that contains text values in several columns. One column has a text value of either true or false. The data type of this column is Text.
Now I want to add a measure that calculates the rows where the column has a value of true. I am using an IF function:
=IF(Table[column a] = "true", COUNTROWS(DISTINCT(columnid)),0)
When doing this, I am getting an error saying that the value cannot be determined in this context. What am I doing wrong?
You are creating a measure that has a context of several rows rather than 1 row, so it can't determine the true/false test in your if statement. Try this DAX for your calculated measure:
MyMeasure:= Calculate(DistinctCount(Table[columnid]), Table[column a] = "True")
Here's a good article on the CALCULATE function in DAX: http://sqlblog.com/blogs/marco_russo/archive/2010/01/03/how-calculate-works-in-dax.aspx

Force mdx query to return column names

When connecting from powerpivot to SSAS, I got the following problem - if for some reason no rows are returned by mdx query, no column names are returned either and powerpivot gives an error. When executing such a query in SSMS I got 0 rows if mdx returns nothing and 2 rows if mdx returns 1 row (column names and the row itself). So - can I somehow force SSAS to return column names?
Query currently looks a bit like this:
SELECT NON EMPTY {[Measures].[Measure1]} ON COLUMNS,
NON EMPTY {Filter([DimLocalDate].[Date].&[20110101]:[DimLocalDate].[Date].&[20120101],
[JobStatus].[JobStatus].&[1] } ON ROWS
FROM [TheCube]
In my app customer can specify dates and status value. If no results are found I would expect an empty result set, but instead I got an error.
Thanks.
re SSIS error: [MEMBER_CAPTION] cannot be found at the datasource.
Thanks your solution helped me.
I had the same issue using SSIS, SSAS to output a flat file report
i'll post an extended example to help others define the dummy set.
Please note that the date is inserted into MDX script dynamically.
SSIS throws an exception when no data exists for the date, and therefore no results are returned.
This messes up the column order
--create a blank set so that data is still returned when there are no results for that date
set [BlankRowSet] as
([Activity Period].[Days].[Day].&[2014-02-02T00:00:00], [Well].[Location].[Location].&[])
--create the active set as the crossjoin between Days, Wells
set [ActiveSet] as
nonempty([Activity Period].[Days].[Day].members * [Well].[Location].[Location].members )
SET [RealSet] as IIF(COUNT([ActiveSet]) > 0, [ActiveSet], {[BlankRowSet]})
select {[Measures].[Total boe]} ON COLUMNS
,[RealSet] ON ROWS
FROM
(select {[Activity Period].[Days].[Day].&[2014-02-02T00:00:00]:[Activity Period].[Days].[Day].& [2014-02-02T00:00:00]} on 0 from [Volumes] )
where
[Scenario].[All Scenarios].[Category].&[PVR Sales Estimates]
I will check before the select if your NonEmpty clause will return a void set, and in that case put on row a dummy set that will be return to powerpivot at least 1 row.
Something like:
WITH
SET [mySet] as NON EMPTY ({Filter([DimLocalDate].[Date].&[20110101]:[DimLocalDate].[Date].&[20120101], [JobStatus].[JobStatus].&[1] })
SET [myRealSet] as IIF(COUNT([mySet]) > 0, [mySet], {[DummySet]})
SELECT NON EMPTY {[Measures].[Measure1]} ON COLUMNS,
[myRealSet] ON ROWS
FROM [TheCube]
would help?

What is the purpose of BOUND COLUMN property of listbox in MS Access?

What is the purpose of BOUND COLUMN property of listbox?
The bound column is a number that represents what column from the row source will be used to set the value of the Control Source (if the list box is bound).
Note that you can’t use a column name here. So you don't set the bound column to a column name, but you must use a column number.
Another issue here is that the column number starts at 1 (not zero). Note that OFTEN the 1st column length is set to zero. That allows you to have a list box with something like
select PartNumber, PartDescripton from tblParts
The list box will display the part description, but if you set the bound column = 1, then the listbox will return the PartNumber despite the fact that the listbox is displaying Descriptions (since you set the length of the 1st column = 0. If you set the bound column = 2, then the listbox will return description. Note that you can grab any column value from the list box by using
([lstBox1].Column)
Note that in the above, the column feature is zero based. So, 1 = 2nd column
It's the column of the data set that is used to set the value of the listbox. For example, if it's bound to a dataset with the query:
select firstname,lastname,userid from users;
then setting the bound column to userid (3 in the above example) will cause the user ID information to be returned as the listbox value.
A bound column is the data that the form is going to save. For instance, if you have a list box or combo box that lists employeeID and employeeName and you set the bound column to 0, the form will save the employee ID number from
the selection and insert that value into the corresponding table.
You can test which value you are referencing this using this vba:
Private Sub ComboBoxName_AfterUpdate()
MsgBox ("bound column is: " & Me.ComboBoxName.BoundColumn & ". value is: " & Me.ComboBoxName.Column(0))'change 0 to whatever number column is bound
End Sub
The bound column rule applies even if the first column is hidden on the form. For instance, the user could select "Mike Jones" from the employee list, but the form is going to save Mike Jones' employeeID for data uses (this ID could be stored in a table of sales records, etc.).