> ## Documentation Index
> Fetch the complete documentation index at: https://developer.mindbridge.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Analyses

This section guides you through creating a general ledger analysis, creating a TRA vendor analysis, retrieving an analysis by its identifier, and querying analyses using the Python SDK.

# Creating a general ledger analysis

To set up an analysis, you first need to create or select an existing [organization](/sdk/python/tutorials/organizations) and [engagement](/sdk/python/tutorials/engagements). For details on how to work with these resources, see their respective guides.

If you omit `library_id` from the `EngagementItem`, the MindBridge SDK creates a library using the `MindBridge for-profit (Mac v2) Library`. That library includes the general ledger, accounts payable, and accounts receivable analysis types by default.

<CodeGroup dropdown>
  ```python theme={null}
  # get-or-create our organization
  organization_name = "Chapter 3 - Creating an Analysis"
  try:
      organization = next(server.organizations.get({"name": organization_name}))
      print(f"Organization '{organization_name}' already exists.")
  except StopIteration:
      new_organization_item = mbapi.OrganizationItem(
          name=organization_name,
          external_client_code="My Client ID",  # Optional
          manager_user_ids=[user.id],  # Optional
      )
      organization_item = mbapi.OrganizationItem(name=organization_name)
      organization = server.organizations.create(organization_item)
      print(f"Organization '{organization_name}' created.")


  engagement_name = "Engagement - MindBridge for-profit Library"
  try:
      new_engagement_item = mbapi.EngagementItem(
          organization_id=organization.id,
          name=engagement_name,
          engagement_lead_id=user.id,
      )
      engagement = server.engagements.create(new_engagement_item)
      print(f"Created the new engagement {engagement.name}")
  except mbapi.exceptions.ValidationError:
      engagement = next(
          server.engagements.get(
              {"organizationId": organization.id, "name": engagement_name}
          )
      )
      print(
          f"Fetched the engagement '{engagement.name}' from the organization with id "
          f"{organization.id}"
      )
  ```
</CodeGroup>

After the engagement **Engagement - MindBridge for-profit Library** exists, create an analysis within it.

<CodeGroup dropdown>
  ```python theme={null}
  from datetime import date

  new_gl_analysis_item = mbapi.AnalysisItem(
      name="GL Analysis",
      engagement_id=engagement.id,
      currency_code="CAD",
      analysis_periods=[
          mbapi.AnalysisPeriod(end_date=date(2024, 12, 31), start_date=date(2024, 1, 1))
      ],
      analysis_type_id=mbapi.AnalysisTypeItem.GENERAL_LEDGER,
  )

  analysis = server.analyses.create(new_gl_analysis_item)

  print(
      f"https://{os.getenv('MINDBRIDGE_URL')}/app/organization/{organization.id}/"
      f"engagement/{engagement.id}/analysis-list"
  )
  ```
</CodeGroup>

At the link printed above, an analysis is ready for data upload.

# Creating a custom analysis

To create a custom analysis, you first need a library with your desired analysis type. Then create an engagement that uses that library.

In this example, you set up a TRA vendor analysis by creating an analysis type from the `TRA Vendor Template` that MindBridge provides.

Create a TRA vendor analysis type in the Analysis Designer:

1. Open the Analysis Designer.
2. Find the analysis type named **TRA Vendor Template**.
3. Duplicate **TRA Vendor Template** and set the name to **SDK TRA Vendor**.
4. Click **Save**.
5. Return to the Analysis Designer page.
6. Select **Publish** on **SDK TRA Vendor**.

Then create a library that includes that analysis type:

1. Open the Libraries page.
2. Select **Create Library**.
3. Set the name to **SDK TRA Vendor Library**.
4. Set the base library to **MindBridge for-profit (MAC v.2)**.
5. Add **SDK TRA Vendor** to the analysis types.
6. Set the account grouping to **Mac v.2**.
7. Select **Create Library**. You should now have a library named **SDK TRA Vendor Library** that contains the analysis type **SDK TRA Vendor**.

Create an engagement that uses this library by setting `library_id` on the `EngagementItem`.

<CodeGroup dropdown>
  ```python theme={null}
  all_libraries = server.libraries.get()
  vendor_library = next(
      library for library in all_libraries if library.name == "SDK TRA Vendor Library"
  )

  organization_name = "Chapter 3 - Creating an Analysis"
  try:
      organization = next(server.organizations.get({"name": organization_name}))
      print(f"Organization '{organization_name}' already exists.")
  except StopIteration:
      new_organization_item = mbapi.OrganizationItem(
          name=organization_name,
          external_client_code="My Client ID",  # Optional
          manager_user_ids=[user.id],  # Optional
      )
      organization_item = mbapi.OrganizationItem(name=organization_name)
      organization = server.organizations.create(organization_item)
      print(f"Organization '{organization_name}' created.")


  engagement_name = "Engagement - SDK TRA Vendor Library"
  try:
      new_engagement_item = mbapi.EngagementItem(
          organization_id=organization.id,
          name=engagement_name,
          engagement_lead_id=user.id,
          library_id=vendor_library.id,
      )
      engagement = server.engagements.create(new_engagement_item)
      print(f"Created the new engagement '{engagement.name}'")
  except mbapi.exceptions.ValidationError:
      engagement = next(
          server.engagements.get(
              {"organizationId": organization.id, "name": engagement_name}
          )
      )
      print(
          f"Fetched the engagement '{engagement.name}' from the organization with id "
          f"{organization.id}"
      )
  ```
</CodeGroup>

After the engagement **Engagement - SDK TRA Vendor Library** exists, create an analysis within it. Fetch the analysis type from the engagement as shown below.

<CodeGroup dropdown>
  ```python theme={null}
  from datetime import date

  all_libraries = server.libraries.get()
  vendor_library = next(
      library for library in all_libraries if library.name == "SDK TRA Vendor Library"
  )
  available_analysis_types = list(vendor_library.analysis_types)

  print(f"These are the Analysis Types available in the library '{vendor_library.name}'")
  for at in available_analysis_types:
      print(at.name)

  tra_vendor_analysis_type = next(
      analysis_type
      for analysis_type in available_analysis_types
      if analysis_type.name == "SDK TRA Vendor"
  )

  new_gl_analysis_item = mbapi.AnalysisItem(
      name="GL Analysis",
      engagement_id=engagement.id,
      currency_code="CAD",
      analysis_periods=[
          mbapi.AnalysisPeriod(end_date=date(2024, 12, 31), start_date=date(2024, 1, 1))
      ],
      analysis_type_id=tra_vendor_analysis_type.id,
  )

  analysis = server.analyses.create(new_gl_analysis_item)

  print(
      f"https://{os.getenv('MINDBRIDGE_URL')}/app/organization/{organization.id}/"
      f"engagement/{engagement.id}/analysis-list"
  )
  ```
</CodeGroup>
