Skip to content

Auto-create DAX measures when adding value fields to OLAP PivotTables #217

@sbroenne

Description

@sbroenne

Problem

Currently, calling add-value-field on an OLAP PivotTable (connected to Data Model) always fails with:

Cannot add value field '[Measures].[Total ACR]' to OLAP PivotTable. 
OLAP measures must be pre-defined in the Excel Data Model.

User's attempted workflow:

{
  "action": "add-value-field",
  "pivotTableName": "ACR_by_Date",
  "fieldName": "[Measures].[Total ACR]",
  "sessionId": "..."
}

Why this is confusing for LLMs:

  1. add-value-field works for regular PivotTables (range/table-based)
  2. add-value-field always fails for OLAP PivotTables (Data Model-based)
  3. 🤔 LLMs have to learn two different workflows for the same conceptual operation
  4. 📚 Error message requires manual intervention (open Excel, create measure, etc.)

Proposed Solution

Make add-value-field smart enough to auto-create DAX measures for OLAP PivotTables:

  1. Detect if PivotTable is OLAP (Data Model-based)
  2. Auto-create DAX measure with appropriate aggregation function
  3. Add the measure to the PivotTable values area
  4. Return success

Benefits:

  • Single unified API - LLMs don't need to detect PivotTable type
  • "It just works" - no confusing error messages
  • Matches LLM mental model - "add this field as sum/count/average"
  • Backward compatible - existing workflows continue working

Implementation Plan

1. In OlapPivotTableFieldStrategy.AddValueField():

// When adding value field to OLAP PivotTable:
// 1. Extract table name and field name from the field reference
// 2. Check if DAX measure already exists (optional - avoid duplicates)
// 3. Auto-create DAX measure based on aggregation function:
//    - Sum → [FieldName Sum] = SUM('TableName'[FieldName])
//    - Count → [FieldName Count] = COUNT('TableName'[FieldName])
//    - Average → [FieldName Average] = AVERAGE('TableName'[FieldName])
//    - etc.
// 4. Add the new measure to PivotTable values area
// 5. Return success

2. Test Coverage

  • ✅ Regular PivotTable + add-value-field (already works)
  • ✅ OLAP PivotTable + add-value-field with Sum (new - auto-create DAX)
  • ✅ OLAP PivotTable + add-value-field with Count (new - auto-create DAX)
  • ✅ OLAP PivotTable + add-value-field with pre-existing measure (should still work)

3. Documentation Updates

  • Update excel_pivottable.md prompt: Remove limitation warning
  • Update tool description: Clarify auto-DAX behavior
  • Update examples: Show OLAP PivotTable workflows

Alternative Considered (Rejected)

Option: Separate excel_olap_pivottable tool

  • ❌ More confusing for LLMs (two APIs for same operation)
  • ❌ More documentation to maintain
  • ❌ Requires LLMs to detect PivotTable type first

Impact

Before:

  • LLMs get error message and must use manual workaround
  • Different workflows for regular vs OLAP PivotTables

After:

  • LLMs use same add-value-field action for all PivotTable types
  • DAX measures auto-created behind the scenes
  • Consistent, intuitive API

Definition of Done

  • Failing test case created (OLAP PivotTable + add-value-field)
  • Implementation in OlapPivotTableFieldStrategy.AddValueField()
  • Test passes after implementation
  • Additional test cases for Count, Average, etc.
  • Documentation updated (prompt files, tool descriptions)
  • Integration test with end-to-end workflow
  • PR created and reviewed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions