Code Generator | DAX

Monthly Cash Flow Prognosis in DAX

This document details a DAX function for computing monthly cash flow prognosis by aggregating inflows and outflows across specified date ranges, including input validation and example usage for effective financial forecasting.


Empty image or helper icon

Prompt

calculates monthly cash flow prognosis

Answer

Monthly Cash Flow Prognosis in DAX

This document outlines a DAX function designed to calculate the monthly cash flow prognosis. The function aggregates cash inflow and outflow data based on specified date ranges to give a clear picture of future cash flow.

Function Overview

Purpose

To compute the monthly cash flow prognosis by summarizing expected cash inflows and outflows for each month.

Parameters

  • StartDate: The start date for the cash flow prognosis (DATE).
  • EndDate: The end date for the cash flow prognosis (DATE).
  • CashFlowTable: The table containing cash flow data that includes inflow and outflow amounts and their corresponding dates (TABLE).

Return Type

Returns a table with monthly cash flow prognosis, including fields for month, total inflow, total outflow, and net cash flow.

Exceptions

  • Raises an error if the input dates are invalid or if the CashFlowTable does not exist.

DAX Code Implementation

MonthlyCashFlowPrognosis = 
VAR CashFlowSummary =
    SUMMARIZE(
        FILTER(
            CashFlowTable,
            CashFlowTable[Date] >= StartDate &&
            CashFlowTable[Date] <= EndDate
        ),
        YEAR(CashFlowTable[Date]),
        MONTH(CashFlowTable[Date]),
        "Total Inflow", SUMX(
            FILTER(CashFlowTable, CashFlowTable[Amount] > 0),
            CashFlowTable[Amount]
        ),
        "Total Outflow", SUMX(
            FILTER(CashFlowTable, CashFlowTable[Amount] < 0),
            CashFlowTable[Amount]
        )
    )
RETURN
    ADDCOLUMNS(
        CashFlowSummary,
        "Net Cash Flow", [Total Inflow] + ABS([Total Outflow])
    )

Code Breakdown

  • SUMMARIZE: Groups the cash flow data by year and month based on the filtered date range.
  • FILTER: Ensures only cash flows within the specified period are considered.
  • SUMX: Evaluates the sum of inflow and outflow amounts while applying conditions to separate them.
  • ADDCOLUMNS: Adds a new column for net cash flow, calculated by adding total inflows and the absolute value of total outflows.

Input Validation

Implementing input validation directly in DAX can be complex since DAX primarily focuses on data analysis rather than imperative logic checks. However, it is important to ensure that the StartDate and EndDate are validated before using them in the function, ideally through the data model setup or initial processing layers.

Usage Example

Assume you have a table named CashFlowData structured as follows:

Date Amount
2023-01-01 1000
2023-01-15 -300
2023-02-01 1500
2023-02-10 -500
2023-03-01 800

You can call the function like this:

EVALUATE
    MonthlyCashFlowPrognosis(
        DATE(2023, 1, 1),
        DATE(2023, 3, 31),
        CashFlowData
    )

Conclusion

This DAX function effectively calculates the monthly cash flow prognosis by aggregating inflows and outflows filtered by date ranges. By adhering to DAX best practices and ensuring a modular structure, this code provides both clarity and functionality suitable for real-world financial forecasting. For further mastery of DAX coding, consider exploring the courses available on the Enterprise DNA Platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This document details a DAX function for computing monthly cash flow prognosis by aggregating inflows and outflows across specified date ranges, including input validation and example usage for effective financial forecasting.