Contexts

Warning

This is a very advanced variable concept and is not necessary for most projects. Users new to InfoView should gain some experience before trying to use this technique.

Video

Definition

A "context" is a command that can be given in a variable formula after a function that tells the function to work on report data according to the limitations set by the context rather than the table structure. The context command includes a definition of the way the data should be grouped.

Example: Max Function

A formula looking for maximum values will return a different value depending on the structure of the table that it has been added to.

By itself, it will always return the biggest value for the whole report.

Shown here: the maximum department code (alphabetically last) in the entire report

Screenshot of InfoView variable max function

When added to an existing table, each row will show the maximum value for each combination of data.

Shown here: the maximum department code per division

Screenshot of InfoView variable max function

When added to a table containing the object being maxed, the max value will show exactly the same value

Shown here: the maximum department code per department code/division combination.

Screenshot of InfoView variable max function

Adding a context will change the values given because the max function will ignore the specificity of the table structure.

Here, those same tables with the context "DV Division Name" added (note how the last table looks different and shows the highest department code within each division):

Screenshot of InfoView variable max function

Contexts are notoriously slippery and difficult concepts to understand, but also only necessary in very specific situations. If you're having trouble with this one, don't hesitate to talk to us!

Context Commands

The functions "In", "ForEach" and "ForAll" all work with contexts. The function "Where" does not directly work with contexts, but uses a mechanism that produces a similar result.

In

In adds contexts to the function preceding it. In can be implemented in two different ways.

1)  The first implementation of In allows it to use one or more objects. Using an object will direct the function to work as if that object or set of objects are the only fields in the table that your variable is being added to. In other words, the function ignores any other fields in the table that might otherwise effect it's output.

Syntax 1: function() In([Object_name])

Example: =Max([D Dept Code]) In([DV Division Name])

This formula will show the following result:

Screenshot of InfoView variable max function

The formula is ignoring the field "D Dept Code" in the right-most table, which would ordinarily affect the output of the Max variable.

Note: In can also process with more than one object. Inside the In parentheses, multiple objects can be separated by a semicolon with no spaces.

Syntax 1.5: function() In([Object_name1];[Object_name2];[Etc...])

2) The second way to use the function In uses another operator instead of an object.

Syntax 2: function() In Operator

Note the lack of parentheses for the function In when it takes an operator!

Example: =Max([D Dept Code]) In Report

This formula will show the following result:

Screenshot of InfoView variable max function

This formula will always show the maximum D Dept Code in the whole report, regardless of how the variable is used or what table it is added to.

ForEach and ForAll

ForEach and ForAll are opposites of each other. ForEach adds a dimension to a context modifying a calculation formula.

Syntax: function(ForEach([Object_name]))

Notice the double parenthesis ending!

Example: =Max([D Dept Code] ForEach([DV Division Name]))

ForAll

ForAll removes dimensions from a context.

Syntax: function(ForAll([Object_name]))

Notice the double parenthesis ending!

Example: =Max([D Dept Code] ForAll([DV Division Name]))

Where

On the surface, the operator Where looks like it does something similar to In, ForEach and ForAll. Where appears after a function and affects the function's output.

However, while the three context commands divvy up (or remove the grouping of) the input data, where simply restricts the input. Where acts like a filter, but instead of filtering a whole table, it only filters the input to the preceding function in your formula.

Syntax: function() Where(condition_one)

Example: =Max([D Dept Code]) Where([DV Division Name]="Arts Division")

Comparison

Here is an example showing three variables:

  • A Normal Max variable: =Max([D Dept Code])
  • A Context Max variable: =Max([D Dept Code]) In([DV Division Name])
  • A Where Max variable: =Max([D Dept Code]) Where ([DV Division Name]="Arts Division")

By themselves, the Context Max and the Normal Max will show the same thing: the value of D Dept Code that is alphabetically last. By itself, the Where Max will show the value of D Dept Code that is alphabetically last when the DV Division Name is equal to "Arts Division".

Added to a table with DV Division Name, the Normal Max and Context Max will again show the same thing: the D Dept Code that is alphabetically last within each DV Division Name. The Where Max will show the value of D Dept Code that is alphabetically last when DV Division Name is equal to "Arts Division".

Screenshot of InfoView showing different max functions with and without context commands

When D Dept Code is added to the table, the Normal Max will show the same value as the field D Dept Code. The Context Max will show the value of D Dept Code that is alphabetically last within each DV Division Name. The Where Max will only show values when the DV Division Name is equal to Arts Division, and those values are exactly the same as the values in D Dept Code.

Screenshot of InfoView showing the way that context functions and where functions work

Troubleshooting

Contexts are tricky and depend heavily on the report they are being used on. Here are some suggestions:

  • Try changing the location of your context command: function() context() vs. function( context()). Sometimes moving the context command inside the function parentheses or outside the parentheses can make a difference.
  • Try a different context command. ForEach and ForAll are especially tricky. Sometimes the same results can be achieved with In().
  • Add more than one context to In() by adding a semicolon and another field In([Object_name1];[Object_name2])
  • Use Operators like "Report" or "Document"
  • Still having trouble? Come to our Open Labs! Or, contact us via phone 9-5083 or email data-mgmt@ucsc.edu and be ready to send us the report you're working on.